-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
62 lines (54 loc) · 1.79 KB
/
session.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
package middlewares
import (
"context"
"log"
"net/http"
"strings"
"github.com/charlieegan3/curry-club/pkg/tool/handlers/public"
"github.com/charlieegan3/curry-club/pkg/tool/stores"
)
func BuildSessionMiddleware(sessionDB *stores.SessionDB, userDB *stores.UserDB) func(http.Handler) http.Handler {
logoutHandler := public.BuildLogoutUserHandler(sessionDB)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session")
if err == nil {
session, authd, err := sessionDB.GetSession(cookie.Value)
if err != nil {
log.Printf("error getting session: %s", err.Error())
logoutHandler(w, r)
return
}
user, err := userDB.GetUserByID(string(session.UserID))
if err != nil {
log.Printf("error getting user: %s", err.Error())
logoutHandler(w, r)
return
}
if !strings.HasPrefix(r.URL.Path, "/register/") && !strings.HasPrefix(r.URL.Path, "/login/") {
log.Println(r.URL.Path)
if len(user.Credentials) == 0 &&
!strings.HasPrefix(r.URL.Path, "/register/") {
// user did not complete registration
log.Printf("user did not complete registration %s, deleting...", user.ID)
err := userDB.DeleteUserByID(user.ID)
if err != nil {
log.Printf("error deleting user: %s", err.Error())
}
logoutHandler(w, r)
return
}
if !authd && !strings.HasPrefix(r.URL.Path, "/login/") {
// user did not complete login
log.Printf("user not authenticated, logging out")
logoutHandler(w, r)
return
}
}
r = r.WithContext(context.WithValue(r.Context(), "userID", user.ID))
r = r.WithContext(context.WithValue(r.Context(), "userName", user.Username))
}
next.ServeHTTP(w, r)
})
}
}