package services import ( "context" "github.com/hay-kot/content/backend/ent" ) 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 *ent.User, 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) *ent.User { if val := ctx.Value(ContextUser); val != nil { return val.(*ent.User) } 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 "" }