Skip to content

Commit

Permalink
feat: add support for more username formats
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Sep 3, 2024
1 parent fd21ce5 commit 903b0b3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
5 changes: 0 additions & 5 deletions backend/internal/dto/user_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,3 @@ type OneTimeAccessTokenCreateDto struct {
UserID string `json:"userId" binding:"required"`
ExpiresAt time.Time `json:"expiresAt" binding:"required"`
}

type LoginUserDto struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
5 changes: 4 additions & 1 deletion backend/internal/dto/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ var validateUrlList validator.Func = func(fl validator.FieldLevel) bool {
}

var validateUsername validator.Func = func(fl validator.FieldLevel) bool {
regex := "^[a-z0-9_]*$"
// [a-zA-Z0-9] : The username must start with an alphanumeric character
// [a-zA-Z0-9_.@-]* : The rest of the username can contain alphanumeric characters, dots, underscores, hyphens, and "@" symbols
// [a-zA-Z0-9]$ : The username must end with an alphanumeric character
regex := "^[a-zA-Z0-9][a-zA-Z0-9_.@-]*[a-zA-Z0-9]$"
matched, _ := regexp.MatchString(regex, fl.Field().String())
return matched
}
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/utils/controller_error_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func handleValidationError(validationErrors validator.ValidationErrors) string {
case "email":
errorMessage = fmt.Sprintf("%s must be a valid email address", fieldName)
case "username":
errorMessage = fmt.Sprintf("%s must contain only lowercase letters, numbers, and underscores", fieldName)
errorMessage = fmt.Sprintf("%s must only contain lowercase letters, numbers, underscores, dots, hyphens, and '@' symbols and not start or end with a special character", fieldName)
case "url":
errorMessage = fmt.Sprintf("%s must be a valid URL", fieldName)
case "min":
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/routes/settings/admin/users/user-form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
.string()
.min(2)
.max(30)
.regex(/^[a-z0-9_]+$/, 'Only lowercase letters, numbers, and underscores are allowed'),
.regex(
/^[a-z0-9_@.-]+$/,
"Username can only contain lowercase letters, numbers, underscores, dots, hyphens, and '@' symbols"
),
email: z.string().email(),
isAdmin: z.boolean()
});
Expand Down

0 comments on commit 903b0b3

Please sign in to comment.