stato intermedio
This commit is contained in:
@@ -22,8 +22,20 @@ var (
|
||||
ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
ErrEmailNotVerified = errors.New("email not verified")
|
||||
ErrInvalidOrExpiredToken = errors.New("invalid or expired token")
|
||||
ErrInvalidLanguage = errors.New("invalid language")
|
||||
ErrInvalidTheme = errors.New("invalid theme")
|
||||
)
|
||||
|
||||
var supportedLanguages = map[string]struct{}{
|
||||
"it": {},
|
||||
"en": {},
|
||||
"en_us": {},
|
||||
"de": {},
|
||||
"fr": {},
|
||||
"de_ch": {},
|
||||
"fr_ch": {},
|
||||
}
|
||||
|
||||
type AuthService struct {
|
||||
cfg *config.Config
|
||||
users *repo.UserRepo
|
||||
@@ -187,6 +199,23 @@ func (s *AuthService) ResetPassword(token, newPassword string) error {
|
||||
return s.resetTokens.DeleteByID(record.ID)
|
||||
}
|
||||
|
||||
func (s *AuthService) UpdateUserLanguage(userID string, lang string) error {
|
||||
normalized := NormalizeLanguage(lang)
|
||||
if !IsSupportedLanguage(normalized) {
|
||||
return ErrInvalidLanguage
|
||||
}
|
||||
|
||||
return s.users.UpsertLanguagePreference(userID, normalized)
|
||||
}
|
||||
|
||||
func (s *AuthService) UpdateUserTheme(userID string, theme string) error {
|
||||
normalized := NormalizeTheme(theme)
|
||||
if normalized != "dark" && normalized != "light" {
|
||||
return ErrInvalidTheme
|
||||
}
|
||||
return s.users.UpsertDarkPreference(userID, normalized == "dark")
|
||||
}
|
||||
|
||||
func (s *AuthService) issueVerifyEmail(ctx context.Context, user *models.User) error {
|
||||
plainToken, err := auth.NewToken()
|
||||
if err != nil {
|
||||
@@ -245,3 +274,17 @@ func (s *AuthService) sendResetEmail(ctx context.Context, user *models.User, pla
|
||||
func normalizeEmail(email string) string {
|
||||
return strings.ToLower(strings.TrimSpace(email))
|
||||
}
|
||||
|
||||
func NormalizeLanguage(lang string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(lang))
|
||||
return strings.ReplaceAll(normalized, "-", "_")
|
||||
}
|
||||
|
||||
func IsSupportedLanguage(lang string) bool {
|
||||
_, ok := supportedLanguages[NormalizeLanguage(lang)]
|
||||
return ok
|
||||
}
|
||||
|
||||
func NormalizeTheme(theme string) string {
|
||||
return strings.ToLower(strings.TrimSpace(theme))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user