package context import ( "context" "errors" pkgErrors "busniess-user-center/pkg/errors" "busniess-user-center/pkg/utils/session" ) type contextSessionKey struct{} type ContextSessionKey struct{} func MustGetSessionAccount(ctx context.Context) string { if account, err := GetSessionAccount(ctx); err == nil { return account } return "" } func GetSessionAccount(ctx context.Context) (account string, err error) { sess, err := GetSession(ctx) if err != nil { return "", errors.New(pkgErrors.UNAUTH) } return sess.Account, nil } func GetSessionUserID(ctx context.Context) (userID string, err error) { sess, err := GetSession(ctx) if err != nil { return "", errors.New(pkgErrors.UNAUTH) } return sess.ID, nil } func GetSession(ctx context.Context) (sess *session.Session, err error) { sess, ok := ctx.Value(contextSessionKey{}).(*session.Session) if ok { return } return nil, errors.New(pkgErrors.UNAUTH) } func PutSession(ctx context.Context, sess *session.Session) (newCtx context.Context) { return context.WithValue(ctx, contextSessionKey{}, sess) }