Files
spend-sparrow/internal/core/auth.go
Tim Wundenberg 75433834ed
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 1m7s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 1m10s
feat: extract authentication to domain package
2025-12-25 07:39:48 +01:00

52 lines
832 B
Go

package core
import (
"net/http"
"spend-sparrow/internal/auth_types"
)
type ContextKey string
var SessionKey ContextKey = "session"
var UserKey ContextKey = "user"
func GetUser(r *http.Request) *auth_types.User {
obj := r.Context().Value(UserKey)
if obj == nil {
return nil
}
user, ok := obj.(*auth_types.User)
if !ok {
return nil
}
return user
}
func GetSession(r *http.Request) *auth_types.Session {
obj := r.Context().Value(SessionKey)
if obj == nil {
return nil
}
session, ok := obj.(*auth_types.Session)
if !ok {
return nil
}
return session
}
func CreateSessionCookie(sessionId string) http.Cookie {
return http.Cookie{
Name: "id",
Value: sessionId,
MaxAge: 60 * 60 * 8, // 8 hours
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Path: "/",
}
}