-
Notifications
You must be signed in to change notification settings - Fork 0
/
signIn.go
67 lines (65 loc) · 1.86 KB
/
signIn.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package handler
import (
"encoding/json"
"fmt"
"github.com/dnielsen/campsite/pkg/config"
"github.com/dnielsen/campsite/pkg/jwt"
"io/ioutil"
"log"
"net/http"
)
// `/auth/sign-in` POST route. It communicates with the auth service only.
func SignIn(client *http.Client, c *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create the request.
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%v:%v/sign-in", c.Service.Auth.Host, c.Service.Auth.Port), r.Body)
if err != nil {
log.Printf("Failed to create new request: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Make the request.
res, err := client.Do(req)
if err != nil {
log.Printf("Failed to do request: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Read the response body.
tokenBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Failed to read response body: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Set the token cookie.
token := string(tokenBytes)
http.SetCookie(w, &http.Cookie{
Name: c.Jwt.CookieName,
Value: token,
Path: "/",
MaxAge: 60 * 60 * 24 * 7,
Secure: false,
HttpOnly: true,
SameSite: 1,
})
// Get the claims.
claims, err := jwt.VerifyToken(token, &c.Jwt)
if err != nil {
log.Printf("Failed to verify token: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Marshal `Me` (auth data).
b, err := json.Marshal(claims.Me)
if err != nil {
log.Printf("Failed to marshal `Me`: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Respond JSON with `Me`
w.Header().Set(CONTENT_TYPE, APPLICATION_JSON)
w.WriteHeader(http.StatusOK)
w.Write(b)
}
}