feat(auth): persist preferred language per user and session

Refs #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:07:00 +01:00
1 parent 7dd1f87ffe
commit dde7b890b8
7 files changed
+60 -15

No files matched your search

+13 -2
View File
@@ -22,6 +22,7 @@ type Session struct {
Token string
UserID uuid.UUID
OrgID uuid.UUID
Locale string
CSRF string
CreatedAt time.Time
}
@@ -56,11 +57,12 @@ func RandomToken() string {
return base64.RawURLEncoding.EncodeToString(b)
}
func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID) (*Session, error) {
func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID, locale string) (*Session, error) {
sess := &Session{
Token: RandomToken(),
UserID: user,
OrgID: org,
Locale: locale,
CSRF: RandomToken(),
CreatedAt: s.now(),
}
@@ -69,6 +71,7 @@ func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID) (*Session, e
p.HSet(ctx, k, map[string]any{
"user": sess.UserID.String(),
"org": orgString(org),
"locale": locale,
"csrf": sess.CSRF,
"created": sess.CreatedAt.Unix(),
})
@@ -120,6 +123,14 @@ func (s *Sessions) SwitchOrg(ctx context.Context, sess *Session, org uuid.UUID)
return nil
}
func (s *Sessions) SetLocale(ctx context.Context, sess *Session, locale string) error {
if err := s.rdb.HSet(ctx, key(sess.Token), "locale", locale).Err(); err != nil {
return fmt.Errorf("auth: set locale: %w", err)
}
sess.Locale = locale
return nil
}
func (s *Sessions) Delete(ctx context.Context, sess *Session) error {
k := key(sess.Token)
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
@@ -163,7 +174,7 @@ func decode(token string, v map[string]string) (*Session, error) {
if err != nil {
return nil, err
}
return &Session{Token: token, UserID: user, OrgID: org, CSRF: v["csrf"], CreatedAt: time.Unix(created, 0)}, nil
return &Session{Token: token, UserID: user, OrgID: org, Locale: v["locale"], CSRF: v["csrf"], CreatedAt: time.Unix(created, 0)}, nil
}
func orgString(id uuid.UUID) string {