mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-17 06:08:42 +00:00
41 lines
1 KiB
Go
41 lines
1 KiB
Go
|
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 ""
|
||
|
}
|