52 lines
832 B
Go
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: "/",
|
|
}
|
|
}
|