feat(auth): add email handling for user authentication and account setup
feat(api): update API methods to include email and identifier for authentication feat(ui): enhance authentication forms to support email input feat(settings): implement language selection in settings dialog feat(language): add language management for multilingual support
This commit is contained in:
1 parent
cac2faae2e
commit
6394374766
9 files changed
+212
-55
No files matched your search
+51
-12
@@ -19,6 +19,7 @@ const (
|
||||
sessionTTL = 24 * time.Hour
|
||||
minPasswordLength = 8
|
||||
maxUsernameLength = 64
|
||||
maxEmailLength = 254
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -26,6 +27,7 @@ var (
|
||||
errSetupComplete = errors.New("Setup has already been completed.")
|
||||
errInvalidCredentials = errors.New("Invalid username or password.")
|
||||
errUsernameRequired = errors.New("Username is required.")
|
||||
errEmailRequired = errors.New("Email is required and must be valid.")
|
||||
errPasswordTooShort = errors.New("Password must be at least 8 characters.")
|
||||
errCurrentPasswordBad = errors.New("Current password is incorrect.")
|
||||
)
|
||||
@@ -47,6 +49,18 @@ func normalizeUsername(value string) (string, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func normalizeEmail(value string) (string, error) {
|
||||
value = strings.ToLower(strings.TrimSpace(value))
|
||||
if value == "" || len([]rune(value)) > maxEmailLength || strings.Count(value, "@") != 1 {
|
||||
return "", errEmailRequired
|
||||
}
|
||||
parts := strings.SplitN(value, "@", 2)
|
||||
if parts[0] == "" || parts[1] == "" || !strings.Contains(parts[1], ".") {
|
||||
return "", errEmailRequired
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func validatePassword(value string) error {
|
||||
if len([]rune(value)) < minPasswordLength {
|
||||
return errPasswordTooShort
|
||||
@@ -159,11 +173,14 @@ type authStatusResponse struct {
|
||||
SetupRequired bool `json:"setupRequired"`
|
||||
Authenticated bool `json:"authenticated"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Email string `json:"email,omitempty"`
|
||||
}
|
||||
|
||||
type credentialsRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Identifier string `json:"identifier"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func (a *AuthService) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -186,7 +203,7 @@ func (a *AuthService) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, authStatusResponse{})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, authStatusResponse{Authenticated: true, Username: user.Username})
|
||||
writeJSON(w, http.StatusOK, authStatusResponse{Authenticated: true, Username: user.Username, Email: user.Email})
|
||||
}
|
||||
|
||||
func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -211,6 +228,11 @@ func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
email, err := normalizeEmail(request.Email)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if err := validatePassword(request.Password); err != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
@@ -221,7 +243,7 @@ func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
}
|
||||
user, err := a.db.createUser(r.Context(), username, string(hash))
|
||||
user, err := a.db.createUser(r.Context(), username, email, string(hash))
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrSingletonUserExists) {
|
||||
writeJSONError(w, http.StatusConflict, errSetupComplete.Error())
|
||||
@@ -234,7 +256,7 @@ func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, authStatusResponse{Authenticated: true, Username: user.Username})
|
||||
writeJSON(w, http.StatusOK, authStatusResponse{Authenticated: true, Username: user.Username, Email: user.Email})
|
||||
}
|
||||
|
||||
func (a *AuthService) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -244,12 +266,19 @@ func (a *AuthService) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
username, err := normalizeUsername(request.Username)
|
||||
if err != nil {
|
||||
identifier := strings.TrimSpace(request.Identifier)
|
||||
if identifier == "" {
|
||||
identifier = strings.TrimSpace(request.Username)
|
||||
}
|
||||
if identifier == "" {
|
||||
writeJSONError(w, http.StatusUnauthorized, errInvalidCredentials.Error())
|
||||
return
|
||||
}
|
||||
user, err := a.db.getUserByUsername(r.Context(), username)
|
||||
if len([]rune(identifier)) > maxEmailLength {
|
||||
writeJSONError(w, http.StatusUnauthorized, errInvalidCredentials.Error())
|
||||
return
|
||||
}
|
||||
user, err := a.db.getUserByIdentifier(r.Context(), identifier)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
@@ -262,7 +291,7 @@ func (a *AuthService) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, authStatusResponse{Authenticated: true, Username: user.Username})
|
||||
writeJSON(w, http.StatusOK, authStatusResponse{Authenticated: true, Username: user.Username, Email: user.Email})
|
||||
}
|
||||
|
||||
func (a *AuthService) handleLogout(w http.ResponseWriter, r *http.Request, _ *AuthUser) {
|
||||
@@ -277,8 +306,9 @@ func (a *AuthService) handleLogout(w http.ResponseWriter, r *http.Request, _ *Au
|
||||
}
|
||||
|
||||
type updateCredentialsRequest struct {
|
||||
CurrentPassword string `json:"currentPassword"`
|
||||
CurrentPassword *string `json:"currentPassword"`
|
||||
NewUsername *string `json:"newUsername"`
|
||||
NewEmail *string `json:"newEmail"`
|
||||
NewPassword *string `json:"newPassword"`
|
||||
}
|
||||
|
||||
@@ -288,7 +318,7 @@ func (a *AuthService) handleUpdateCredentials(w http.ResponseWriter, r *http.Req
|
||||
writeJSONError(w, http.StatusBadRequest, "Invalid JSON body")
|
||||
return
|
||||
}
|
||||
if bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(request.CurrentPassword)) != nil {
|
||||
if request.CurrentPassword != nil && *request.CurrentPassword != "" && bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(*request.CurrentPassword)) != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, errCurrentPasswordBad.Error())
|
||||
return
|
||||
}
|
||||
@@ -301,6 +331,15 @@ func (a *AuthService) handleUpdateCredentials(w http.ResponseWriter, r *http.Req
|
||||
return
|
||||
}
|
||||
}
|
||||
email := user.Email
|
||||
if request.NewEmail != nil {
|
||||
normalized, err := normalizeEmail(*request.NewEmail)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
email = normalized
|
||||
}
|
||||
|
||||
passwordHash := user.PasswordHash
|
||||
if request.NewPassword != nil && *request.NewPassword != "" {
|
||||
@@ -316,7 +355,7 @@ func (a *AuthService) handleUpdateCredentials(w http.ResponseWriter, r *http.Req
|
||||
passwordHash = string(hash)
|
||||
}
|
||||
|
||||
if err := a.db.updateUserCredentials(r.Context(), user.ID, username, passwordHash); err != nil {
|
||||
if err := a.db.updateUserCredentials(r.Context(), user.ID, username, email, passwordHash); err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user