43 lines
915 B
Go
43 lines
915 B
Go
package sushi
|
|
|
|
// IsAuthenticated checks if the current request is from an authenticated user
|
|
func (ctx Ctx) IsAuthenticated() bool {
|
|
user := ctx.UserValue("user")
|
|
return user != nil
|
|
}
|
|
|
|
// GetCurrentUser returns the current authenticated user
|
|
func (ctx Ctx) GetCurrentUser() any {
|
|
return ctx.UserValue("user")
|
|
}
|
|
|
|
// Login authenticates a user session
|
|
func (ctx Ctx) Login(userID int, user any) {
|
|
sess := GetCurrentSession(ctx)
|
|
if sess != nil {
|
|
sess.SetUserID(userID)
|
|
sess.RegenerateID()
|
|
StoreSession(sess)
|
|
|
|
ctx.SetUserValue(SessionCtxKey, sess)
|
|
ctx.SetUserValue("user", user)
|
|
|
|
SetSessionCookie(ctx, sess.ID)
|
|
}
|
|
}
|
|
|
|
// Logout clears the user session
|
|
func (ctx Ctx) Logout() {
|
|
sess := GetCurrentSession(ctx)
|
|
if sess != nil {
|
|
sess.SetUserID(0)
|
|
sess.RegenerateID()
|
|
StoreSession(sess)
|
|
|
|
ctx.SetUserValue(SessionCtxKey, sess)
|
|
SetSessionCookie(ctx, sess.ID)
|
|
}
|
|
|
|
ctx.SetUserValue("user", nil)
|
|
}
|