fix user setting expiring immediately

This commit is contained in:
partisan 2024-08-21 11:08:37 +02:00
parent 3b4b9ee83a
commit a7e1ef812e

View file

@ -3,6 +3,7 @@ package main
import ( import (
"html/template" "html/template"
"net/http" "net/http"
"time"
) )
type UserSettings struct { type UserSettings struct {
@ -39,10 +40,13 @@ func loadUserSettings(r *http.Request) UserSettings {
} }
func saveUserSettings(w http.ResponseWriter, settings UserSettings) { func saveUserSettings(w http.ResponseWriter, settings UserSettings) {
expiration := time.Now().Add(90 * 24 * time.Hour) // 90 days from now
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: "theme", Name: "theme",
Value: settings.Theme, Value: settings.Theme,
Path: "/", Path: "/",
Expires: expiration, // Expiration time needs to be set otherwise it will expire immediately
Secure: true, // Ensure cookie is sent over HTTPS only Secure: true, // Ensure cookie is sent over HTTPS only
SameSite: http.SameSiteNoneMode, // Set SameSite to None SameSite: http.SameSiteNoneMode, // Set SameSite to None
}) })
@ -50,6 +54,7 @@ func saveUserSettings(w http.ResponseWriter, settings UserSettings) {
Name: "language", Name: "language",
Value: settings.Language, Value: settings.Language,
Path: "/", Path: "/",
Expires: expiration,
Secure: true, // Ensure cookie is sent over HTTPS only Secure: true, // Ensure cookie is sent over HTTPS only
SameSite: http.SameSiteNoneMode, // Set SameSite to None SameSite: http.SameSiteNoneMode, // Set SameSite to None
}) })
@ -57,6 +62,7 @@ func saveUserSettings(w http.ResponseWriter, settings UserSettings) {
Name: "safe", Name: "safe",
Value: settings.SafeSearch, Value: settings.SafeSearch,
Path: "/", Path: "/",
Expires: expiration,
Secure: true, // Ensure cookie is sent over HTTPS only Secure: true, // Ensure cookie is sent over HTTPS only
SameSite: http.SameSiteNoneMode, // Set SameSite to None SameSite: http.SameSiteNoneMode, // Set SameSite to None
}) })