diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index d5712eb50..addb6a9d2 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -24,7 +24,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "golang.org/x/crypto/bcrypt" // Import the postgres database driver. - _ "github.com/lib/pq" + "github.com/lib/pq" ) // Database represents an account database @@ -118,7 +118,8 @@ func (d *Database) SetDisplayName( } // CreateAccount makes a new account with the given login name and password, and creates an empty profile -// for this account. If no password is supplied, the account will be a passwordless account. +// for this account. If no password is supplied, the account will be a passwordless account. If the +// account already exists, it will return nil, nil. func (d *Database) CreateAccount( ctx context.Context, localpart, plaintextPassword string, ) (*authtypes.Account, error) { @@ -127,6 +128,12 @@ func (d *Database) CreateAccount( return nil, err } if err := d.profiles.insertProfile(ctx, localpart); err != nil { + if err, ok := err.(*pq.Error); ok { + if err.Code.Class() == "23" { + // 23 => unique_violation => Account already exists + return nil, nil + } + } return nil, err } return d.accounts.insertAccount(ctx, localpart, hash) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go index 6bcff84a3..3068f521d 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -457,25 +457,17 @@ func completeRegistration( } } - avail, err := accountDB.CheckAccountAvailability(ctx, username) - if err == nil && !avail { - 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{ Code: 500, JSON: jsonerror.Unknown("failed to create account: " + err.Error()), } + } else if acc == nil { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.UserInUse("Desired user ID is already taken."), + } } token, err := auth.GenerateAccessToken()