-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
golfer.go
35 lines (30 loc) · 887 Bytes
/
golfer.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
package middleware
import (
"net/http"
"time"
"github.com/code-golf/code-golf/golfer"
"github.com/code-golf/code-golf/session"
"github.com/gofrs/uuid/v5"
)
// Golfer adds the golfer to the context if logged in.
func Golfer(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cookie, _ := r.Cookie("__Host-session"); cookie != nil {
sessionID := uuid.FromStringOrNil(cookie.Value)
if golfer := golfer.Get(session.Database(r), sessionID); golfer != nil {
session.Get(r).Golfer = golfer
// Refresh the cookie.
http.SetCookie(w, &http.Cookie{
HttpOnly: true,
MaxAge: int(30 * 24 * time.Hour / time.Second),
Name: "__Host-session",
Path: "/",
SameSite: http.SameSiteLaxMode,
Secure: true,
Value: cookie.Value,
})
}
}
next.ServeHTTP(w, r)
})
}