Signup: improve error when user already exists
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func PostSignupHandler(ac appcontext.AppContext) {
|
||||
@@ -31,6 +32,21 @@ func PostSignupHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
var existingUser model.User
|
||||
err = ac.Db.Where("name = ?", user.Username).First(&existingUser).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac,
|
||||
myvalidator.HttpError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Err: errors.New("An user with this name already exists."),
|
||||
})
|
||||
return
|
||||
}
|
||||
err = ac.Db.Model(&model.User{}).Save(&userDb).Error
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
|
||||
Reference in New Issue
Block a user