User registration return M_USER_IN_USE when username is already taken

This commit is contained in:
Crom (Thibaut CHARLES) 2017-12-10 15:47:17 +01:00
parent c3cb6f8767
commit 1fb3950e6b
No known key found for this signature in database
GPG key ID: 45A3D5F880B9E6D0
2 changed files with 19 additions and 0 deletions

View file

@ -103,6 +103,12 @@ func InvalidUsername(msg string) *MatrixError {
return &MatrixError{"M_INVALID_USERNAME", msg}
}
// InvalidUsername is an error returned when the client tries to register an
// username that already exists
func UserInUse(msg string) *MatrixError {
return &MatrixError{"M_USER_IN_USE", msg}
}
// GuestAccessForbidden is an error which is returned when the client is
// forbidden from accessing a resource as a guest.
func GuestAccessForbidden(msg string) *MatrixError {

View file

@ -457,6 +457,19 @@ func completeRegistration(
}
}
avail, err := accountDB.CheckAccountAvailability(ctx, username)
if err == nil && avail == false {
return util.JSONResponse{
Code: 400,
JSON: jsonerror.UserInUse("Desired user ID is already taken."),
}
} else if err != nil {
return util.JSONResponse{
Code: 500,
JSON: jsonerror.Unknown("Failed to check account availability: " + err.Error()),
}
}
acc, err := accountDB.CreateAccount(ctx, username, password)
if err != nil {
return util.JSONResponse{