-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_example_test.go
More file actions
40 lines (31 loc) · 992 Bytes
/
Copy pathhandler_example_test.go
File metadata and controls
40 lines (31 loc) · 992 Bytes
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
package oauth_test
import (
"fmt"
"net/http"
"github.com/minetest-go/oauth"
)
func ExampleOauthHandler() {
// OAuth config
cfg := &oauth.OAuthConfig{
Provider: oauth.ProviderTypeGithub,
ClientID: "1234",
Secret: "much-secret-very-secure",
CallbackURL: "http://localhost:8080/api/oauth_callback/github",
}
// user callback handler
cb := func(w http.ResponseWriter, r *http.Request, user_info *oauth.OauthUserInfo) error {
//TODO: register / login user here
fmt.Printf("ID: %s, Name: %s, AvatarURL: %s", user_info.ExternalID, user_info.Name, user_info.AvatarURL)
return nil
}
// create a new oauth handler instance
handler := oauth.NewHandler(cb, cfg)
// register handler on above callback-url
http.Handle("/api/oauth_callback/github", handler)
// navigate the user to the login-url
fmt.Printf("Login-URL: '%s'", handler.LoginURL())
// start webserver
server := &http.Server{Addr: ":8080", Handler: nil}
server.ListenAndServe()
// profit?
}