Compare commits
No commits in common. "master" and "v1.1.1" have entirely different histories.
@ -7,22 +7,12 @@ import (
|
|||||||
|
|
||||||
const UserCtxKey = "user"
|
const UserCtxKey = "user"
|
||||||
|
|
||||||
// Auth holds the authentication middleware and user lookup function
|
// Middleware adds authentication handling
|
||||||
type Auth struct {
|
func Middleware(userLookup func(int) any) sushi.Middleware {
|
||||||
userLookup func(int) any
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a new Auth instance
|
|
||||||
func New(userLookup func(int) any) *Auth {
|
|
||||||
return &Auth{userLookup: userLookup}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Middleware returns the authentication middleware function
|
|
||||||
func (a *Auth) Middleware() sushi.Middleware {
|
|
||||||
return func(ctx sushi.Ctx, next func()) {
|
return func(ctx sushi.Ctx, next func()) {
|
||||||
sess := sushi.GetCurrentSession(ctx)
|
sess := sushi.GetCurrentSession(ctx)
|
||||||
if sess != nil && sess.UserID > 0 && a.userLookup != nil {
|
if sess != nil && sess.UserID > 0 && userLookup != nil {
|
||||||
user := a.userLookup(sess.UserID)
|
user := userLookup(sess.UserID)
|
||||||
if user != nil {
|
if user != nil {
|
||||||
ctx.SetUserValue(UserCtxKey, user)
|
ctx.SetUserValue(UserCtxKey, user)
|
||||||
} else {
|
} else {
|
||||||
@ -34,15 +24,6 @@ func (a *Auth) Middleware() sushi.Middleware {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update refreshes the current user data in the context
|
|
||||||
func (a *Auth) Update(ctx sushi.Ctx) {
|
|
||||||
sess := sushi.GetCurrentSession(ctx)
|
|
||||||
if sess != nil && sess.UserID > 0 && a.userLookup != nil {
|
|
||||||
user := a.userLookup(sess.UserID)
|
|
||||||
ctx.SetUserValue(UserCtxKey, user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequireAuth middleware that redirects unauthenticated users
|
// RequireAuth middleware that redirects unauthenticated users
|
||||||
func RequireAuth(redirectPath ...string) sushi.Middleware {
|
func RequireAuth(redirectPath ...string) sushi.Middleware {
|
||||||
redirect := "/login"
|
redirect := "/login"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user