Skip to content

Commit

Permalink
fix: try fixing session
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Urban committed Apr 30, 2024
1 parent ec8ea03 commit b50cb12
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion backend/internal/handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
return
}
session, _ := store.Get(r, "session-cook")
slog.Info("New?: ", session.IsNew)
session.Values["authenticated"] = true
session.Values["user"] = inputUser.Email
session.Save(r, w)
Expand Down Expand Up @@ -230,6 +231,19 @@ func (h *Handler) HandleAuthenticate(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
session.Options = &sessions.Options{
Path: "/", // Available across the entire domain
MaxAge: 3600, // Expires after 1 hour
HttpOnly: true, // Not accessible via JavaScript
Secure: true, // Only sent over HTTPS
SameSite: http.SameSiteNoneMode, // Controls cross-site request behavior
Domain: r.Host,
}
if err := session.Save(r, w); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

slog.Info("created new session with id", session.ID)

// If the user cannot be read from the cookie, redirect to /login with the site URL as a parameter
Expand All @@ -239,7 +253,6 @@ func (h *Handler) HandleAuthenticate(w http.ResponseWriter, r *http.Request) {
}
slog.Info("Incoming session is authenticated")
sessionUser, ok := session.Values["user"].(string)
session.Save(r, w)
slog.Info(sessionUser)
if !ok {
h.logError(w, "error while fetching user details from session", err, http.StatusInternalServerError)
Expand Down

0 comments on commit b50cb12

Please sign in to comment.