-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
48 lines (42 loc) · 1.26 KB
/
main.go
File metadata and controls
48 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
tumoidc "github.com/robertjndw/go-tum-login-oidc"
)
func main() {
// Initialize OIDC client
oidcClient, err := tumoidc.New(context.Background(),
os.Getenv("TUM_CLIENT_ID"),
tumoidc.WithClientSecret(os.Getenv("TUM_CLIENT_SECRET")),
tumoidc.WithRedirectURL("http://localhost:8080/callback"),
tumoidc.WithScopes("profile", "email"),
)
if err != nil {
log.Fatal("Failed to create OIDC client:", err)
}
// Create HTTP handler
handler := tumoidc.NewHTTPHandler(oidcClient)
http.Handle("/login", handler.Login())
// http.Handle("/callback", handler.HandleCallback())
// More advanced with user information processing
http.Handle("/callback", handler.HandleCallback(func(w http.ResponseWriter, r *http.Request, user *tumoidc.UserInfo) {
// Do something with the user information
fmt.Println("User authenticated:", user)
// Redirect to home page after successful login
http.Redirect(w, r, "/", http.StatusFound)
}))
http.Handle("/logout", handler.Logout())
srv := &http.Server{
Addr: ":8080",
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
log.Println("Server starting on :8080")
log.Fatal(srv.ListenAndServe())
}