/
auth.go
40 lines (38 loc) · 1.04 KB
/
auth.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
package handler
import (
"encoding/json"
"github.com/dnielsen/campsite/pkg/config"
"github.com/dnielsen/campsite/pkg/jwt"
"log"
"net/http"
)
// `/auth` GET route.
func Auth(c *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Try to get claims from the token if exists.
tokenCookie, err := r.Cookie(c.Jwt.CookieName)
if err != nil {
log.Printf("Failed to get cookie: %v", err)
// err != nil == no cookie == not signed in
w.WriteHeader(http.StatusOK)
return
}
claims, err := jwt.VerifyToken(tokenCookie.Value, &c.Jwt)
if err != nil {
log.Printf("Failed to verify token: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Marshal the `Me`.
b, err := json.Marshal(claims.Me)
if err != nil {
log.Printf("Failed to marshal claims: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Respond JSON with the auth data (`Me`)
w.Header().Set(CONTENT_TYPE, APPLICATION_JSON)
w.WriteHeader(http.StatusOK)
w.Write(b)
}
}