Added authentication middleware
This commit is contained in:
11
contexts/keys.go
Normal file
11
contexts/keys.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package contexts
|
||||
|
||||
type contextKey string
|
||||
|
||||
func (c contextKey) String() string {
|
||||
return "projectreshoot context key " + string(c)
|
||||
}
|
||||
|
||||
var (
|
||||
contextKeyAuthorizedUser = contextKey("auth-user")
|
||||
)
|
||||
20
contexts/user.go
Normal file
20
contexts/user.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user