homebox/backend/internal/services/contexts.go
2022-08-29 18:40:54 -08:00

40 lines
1 KiB
Go

package services
import (
"context"
"github.com/hay-kot/content/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 ""
}