-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.go
More file actions
65 lines (57 loc) · 1.37 KB
/
example.go
File metadata and controls
65 lines (57 loc) · 1.37 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"net/http"
"os"
"github.com/borud/gin/pkg/auth"
)
const (
listenAddr = ":3000"
)
func main() {
googleAuth := auth.New(&auth.GoogleAuthConfig{
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
CallbackURL: "http://localhost:3000/google/callback",
LoginCallback: loginCallback,
})
http.HandleFunc("/", mainHandler)
http.HandleFunc("/error", errorHandler)
http.HandleFunc("/google/login", googleAuth.GoogleLoginHandler)
http.HandleFunc("/google/callback", googleAuth.GoogleCallbackHandler)
http.ListenAndServe(listenAddr, nil)
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, `
<html>
<head>
</head>
<body>
<h1>Welcome</h1>
<a href="/google/login">Login</a>
</body>
</html>
`)
}
func errorHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, `
<html>
<head>
</head>
<body>
<h1>Error</h1>
</body>
</html>
`)
}
func loginCallback(w http.ResponseWriter, r *http.Request, userinfo *auth.Userinfo) {
html := `
<html>
<head><title>Wohoo></title></head>
<body>Hello %s<p><img src="%s"</body>
</html>`
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, html, userinfo.Name, userinfo.Picture)
}