|
|
|
@ -389,6 +389,7 @@ func handleUserLoginPost(logger *slog.Logger, tc *TemplateCache, sm *scs.Session
|
|
|
|
|
id, err := userService.Authenticate(form.Email, form.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, model.ErrInvalidCredentials) {
|
|
|
|
|
logAuthFailure(logger, r, form.Email)
|
|
|
|
|
form.AddNonFieldError("Email or password is incorrect")
|
|
|
|
|
|
|
|
|
|
data := newTemplateData(r, sm)
|
|
|
|
@ -414,13 +415,32 @@ func handleUserLoginPost(logger *slog.Logger, tc *TemplateCache, sm *scs.Session
|
|
|
|
|
|
|
|
|
|
// Add the ID of the current user to the session, so that they are now "logged in"
|
|
|
|
|
sm.Put(r.Context(), "authenticatedUserID", id)
|
|
|
|
|
|
|
|
|
|
logAuthSuccess(logger, r, form.Email, id)
|
|
|
|
|
http.Redirect(w, r, "/snippet/create", http.StatusSeeOther)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleUserLogoutPost() http.Handler {
|
|
|
|
|
func handleUserLogoutPost(logger *slog.Logger, sm *scs.SessionManager) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
fmt.Fprintln(w, "Logout the user")
|
|
|
|
|
// Use RenewToken on the current session to change the session ID
|
|
|
|
|
err := sm.RenewToken(r.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, r, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userId := sm.GetString(r.Context(), "authenticatedUserID")
|
|
|
|
|
if userId == "" {
|
|
|
|
|
logger.Info("No athenticated user in session")
|
|
|
|
|
} else {
|
|
|
|
|
logger.Info(fmt.Sprintf("Logging out user: %s", userId))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the authenticatedUserID from the session data
|
|
|
|
|
sm.Remove(r.Context(), "authenticatedUserID")
|
|
|
|
|
|
|
|
|
|
// Add a flash message
|
|
|
|
|
sm.Put(r.Context(), "flash", "You've been logged out successfully!")
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|