Added authentication middleware

This commit is contained in:
2025-02-11 23:46:25 +11:00
parent 97aabcf06f
commit 732f8510ae
12 changed files with 208 additions and 21 deletions

20
contexts/user.go Normal file
View File

@@ -0,0 +1,20 @@
package contexts
import (
"context"
"projectreshoot/db"
)
// Return a new context with the user added in
func SetUser(ctx context.Context, u *db.User) context.Context {
return context.WithValue(ctx, contextKeyAuthorizedUser, u)
}
// Retrieve a user from the given context. Returns nil if not set
func GetUser(ctx context.Context) *db.User {
user, ok := ctx.Value(contextKeyAuthorizedUser).(*db.User)
if !ok {
return nil
}
return user
}