Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
4bad13510a | |||
32116905b7 |
@ -7,12 +7,22 @@ import (
|
|||||||
|
|
||||||
const UserCtxKey = "user"
|
const UserCtxKey = "user"
|
||||||
|
|
||||||
// Middleware adds authentication handling
|
// Auth holds the authentication middleware and user lookup function
|
||||||
func Middleware(userLookup func(int) any) sushi.Middleware {
|
type Auth struct {
|
||||||
|
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 && userLookup != nil {
|
if sess != nil && sess.UserID > 0 && a.userLookup != nil {
|
||||||
user := userLookup(sess.UserID)
|
user := a.userLookup(sess.UserID)
|
||||||
if user != nil {
|
if user != nil {
|
||||||
ctx.SetUserValue(UserCtxKey, user)
|
ctx.SetUserValue(UserCtxKey, user)
|
||||||
} else {
|
} else {
|
||||||
@ -24,6 +34,15 @@ func Middleware(userLookup func(int) any) 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"
|
||||||
|
4
forms.go
4
forms.go
@ -101,10 +101,10 @@ func (f FormValue) IsEmpty() bool {
|
|||||||
// GetFormArray gets multiple form values as string slice
|
// GetFormArray gets multiple form values as string slice
|
||||||
func (ctx Ctx) GetFormArray(key string) []string {
|
func (ctx Ctx) GetFormArray(key string) []string {
|
||||||
var values []string
|
var values []string
|
||||||
ctx.PostArgs().VisitAll(func(k, v []byte) {
|
for k, v := range ctx.PostArgs().All() {
|
||||||
if string(k) == key {
|
if string(k) == key {
|
||||||
values = append(values, string(v))
|
values = append(values, string(v))
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,10 @@ func (s *Session) GetFlash(key string) (any, bool) {
|
|||||||
return value, exists
|
return value, exists
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) DeleteFlash(key string) {
|
||||||
|
s.Delete("flash_" + key)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Session) GetFlashMessage(key string) string {
|
func (s *Session) GetFlashMessage(key string) string {
|
||||||
if flash, exists := s.GetFlash(key); exists {
|
if flash, exists := s.GetFlash(key); exists {
|
||||||
if msg, ok := flash.(string); ok {
|
if msg, ok := flash.(string); ok {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user