forked from mirrors/homebox
Initial commit
This commit is contained in:
commit
29f583e936
135 changed files with 18463 additions and 0 deletions
40
backend/internal/services/contexts.go
Normal file
40
backend/internal/services/contexts.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hay-kot/git-web-template/backend/internal/types"
|
||||
)
|
||||
|
||||
type contextKeys struct {
|
||||
name string
|
||||
}
|
||||
|
||||
var (
|
||||
ContextUser = &contextKeys{name: "User"}
|
||||
ContextUserToken = &contextKeys{name: "UserToken"}
|
||||
)
|
||||
|
||||
// SetUserCtx is a helper function that sets the ContextUser and ContextUserToken
|
||||
// values within the context of a web request (or any context).
|
||||
func SetUserCtx(ctx context.Context, user *types.UserOut, token string) context.Context {
|
||||
ctx = context.WithValue(ctx, ContextUser, user)
|
||||
ctx = context.WithValue(ctx, ContextUserToken, token)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// UseUserCtx is a helper function that returns the user from the context.
|
||||
func UseUserCtx(ctx context.Context) *types.UserOut {
|
||||
if val := ctx.Value(ContextUser); val != nil {
|
||||
return val.(*types.UserOut)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UseTokenCtx is a helper function that returns the user token from the context.
|
||||
func UseTokenCtx(ctx context.Context) string {
|
||||
if val := ctx.Value(ContextUserToken); val != nil {
|
||||
return val.(string)
|
||||
}
|
||||
return ""
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue