chore(auth): #331 add first session test
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 39s

This commit is contained in:
2024-12-22 22:14:55 +01:00
parent ea653f0087
commit 0246ada70e
7 changed files with 55 additions and 19 deletions

View File

@@ -21,15 +21,23 @@ func Authenticate(service service.Auth) func(http.Handler) http.Handler {
sessionId := getSessionID(r)
session, user, _ := service.SignInSession(sessionId)
if session != nil {
// Always sign in anonymous
// This way, we can always generate csrf tokens
if session == nil {
session, err := service.SignInAnonymous()
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
ctx = context.WithValue(ctx, UserKey, user)
ctx = context.WithValue(ctx, SessionKey, session)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
next.ServeHTTP(w, r)
cookie := CreateSessionCookie(session.Id)
http.SetCookie(w, &cookie)
}
ctx = context.WithValue(ctx, UserKey, user)
ctx = context.WithValue(ctx, SessionKey, session)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}