chore: remove username field from user table

This commit is contained in:
Steven
2023-06-23 01:01:22 +08:00
parent 8dfae8a6aa
commit 2aae515544
11 changed files with 52 additions and 78 deletions

View File

@ -21,12 +21,12 @@ func getUserIDContextKey() string {
}
type SignInRequest struct {
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
}
type SignUpRequest struct {
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
}
@ -39,20 +39,20 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
}
user, err := s.Store.GetUser(ctx, &store.FindUser{
Username: &signin.Username,
Email: &signin.Email,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by username %s", signin.Username)).SetInternal(err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by email %s", signin.Email)).SetInternal(err)
}
if user == nil {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found with username %s", signin.Username))
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found with email %s", signin.Email))
} else if user.RowStatus == store.Archived {
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with username %s", signin.Username))
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with email %s", signin.Email))
}
// Compare the stored hashed password, with the hashed version of the password that was received.
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(signin.Password)); err != nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Unmatched username and password").SetInternal(err)
return echo.NewHTTPError(http.StatusUnauthorized, "Unmatched email and password").SetInternal(err)
}
if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil {
@ -74,8 +74,8 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
}
create := &store.User{
Username: signup.Username,
Nickname: signup.Username,
Email: signup.Email,
Nickname: signup.Email,
PasswordHash: string(passwordHash),
}
existingUsers, err := s.Store.ListUsers(ctx, &store.FindUser{})

View File

@ -42,30 +42,25 @@ type User struct {
RowStatus RowStatus `json:"rowStatus"`
// Domain specific fields
Username string `json:"username"`
Nickname string `json:"nickname"`
Email string `json:"email"`
Nickname string `json:"nickname"`
Role Role `json:"role"`
}
type CreateUserRequest struct {
Username string `json:"username"`
Nickname string `json:"nickname"`
Email string `json:"email"`
Nickname string `json:"nickname"`
Password string `json:"password"`
Role Role `json:"-"`
}
func (create CreateUserRequest) Validate() error {
if len(create.Username) < 3 {
return fmt.Errorf("username is too short, minimum length is 3")
}
if create.Nickname != "" && len(create.Nickname) < 3 {
return fmt.Errorf("username is too short, minimum length is 3")
}
if create.Email != "" && !validateEmail(create.Email) {
return fmt.Errorf("invalid email format")
}
if create.Nickname != "" && len(create.Nickname) < 3 {
return fmt.Errorf("nickname is too short, minimum length is 3")
}
if len(create.Password) < 3 {
return fmt.Errorf("password is too short, minimum length is 3")
}
@ -228,9 +223,8 @@ func convertUserFromStore(user *store.User) *User {
CreatedTs: user.CreatedTs,
UpdatedTs: user.UpdatedTs,
RowStatus: RowStatus(user.RowStatus),
Username: user.Username,
Nickname: user.Nickname,
Email: user.Email,
Nickname: user.Nickname,
Role: Role(user.Role),
}
}