-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.go
41 lines (33 loc) · 842 Bytes
/
routes.go
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
package oauth
import (
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"github.com/jesseokeya/go-rest-api-template/server/auth"
)
func GetPing(w http.ResponseWriter, r *http.Request) {
token, _, err := jwtauth.FromContext(r.Context())
if err != nil {
fmt.Fprintf(w, "Permission: %v", err)
return
}
userId, _ := token.Get("userId")
fmt.Fprintf(w, "User ID: %v", userId)
}
func (o *OAuth) Routes() chi.Router {
r := chi.NewRouter()
r.Post("/signin", o.Signin)
r.Post("/signup", auth.Signup)
r.Group(func(r chi.Router) {
// Seek, verify and validate JWT tokens
r.Use(jwtauth.Verifier(o.Authority()))
// Handle valid / invalid tokens.
r.Use(auth.Authenticator)
r.Get("/ping", GetPing)
r.Get("/authorize", o.Authorize)
})
r.Get("/me", o.GetMe)
r.Post("/token", o.GetToken)
return r
}