Fix user registration bug
MODIFY
userapi/internal/api.go
userapi/internal/api.go
@@ -87,7 +87,7 @@ func (a *UserInternalAPI) PerformAccountCreation(ctx context.Context, req *api.P
ServerName: a.ServerName,
UserID: fmt.Sprintf("@%s:%s", req.Localpart, a.ServerName),
}
- return nil
+ return err
}
if err = a.AccountDB.SetDisplayName(ctx, req.Localpart, req.Localpart); err != nil {

MODIFY
userapi/storage/accounts/postgres/storage.go
userapi/storage/accounts/postgres/storage.go
@@ -170,8 +170,8 @@ func (d *Database) CreateAccount(
func (d *Database) createAccount(
ctx context.Context, txn *sql.Tx, localpart, plaintextPassword, appserviceID string,
) (*api.Account, error) {
+ var account *api.Account
var err error
-
// Generate a password hash if this is not a password-less user
hash := ""
if plaintextPassword != "" {
@@ -180,14 +180,16 @@ func (d *Database) createAccount(
return nil, err
}
}
- if err := d.profiles.insertProfile(ctx, txn, localpart); err != nil {
+ if account, err = d.accounts.insertAccount(ctx, txn, localpart, hash, appserviceID); err != nil {
if sqlutil.IsUniqueConstraintViolationErr(err) {
return nil, sqlutil.ErrUserExists
}
return nil, err
}
-
- if err := d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{
+ if err = d.profiles.insertProfile(ctx, txn, localpart); err != nil {
+ return nil, err
+ }
+ if err = d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{
"global": {
"content": [],
"override": [],
@@ -198,7 +200,7 @@ func (d *Database) createAccount(
}`)); err != nil {
return nil, err
}
- return d.accounts.insertAccount(ctx, txn, localpart, hash, appserviceID)
+ return account, nil
}
// SaveAccountData saves new account data for a given user and a given room.

DELETE
userapi/storage/accounts/sqlite3/constraint.go
-// Copyright 2020 The Matrix.org Foundation C.I.C.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// +build !wasm
-
-package sqlite3
-
-import (
- "errors"
-
- "github.com/mattn/go-sqlite3"
-)
-
-func isConstraintError(err error) bool {
- return errors.Is(err, sqlite3.ErrConstraint)
-}

MODIFY
userapi/storage/accounts/sqlite3/storage.go
userapi/storage/accounts/sqlite3/storage.go
@@ -204,6 +204,7 @@ func (d *Database) createAccount(
ctx context.Context, txn *sql.Tx, localpart, plaintextPassword, appserviceID string,
) (*api.Account, error) {
var err error
+ var account *api.Account
// Generate a password hash if this is not a password-less user
hash := ""
if plaintextPassword != "" {
@@ -212,14 +213,13 @@ func (d *Database) createAccount(
return nil, err
}
}
- if err := d.profiles.insertProfile(ctx, txn, localpart); err != nil {
- if isConstraintError(err) {
+ if account, err = d.accounts.insertAccount(ctx, txn, localpart, hash, appserviceID); err != nil {
return nil, sqlutil.ErrUserExists
}
+ if err = d.profiles.insertProfile(ctx, txn, localpart); err != nil {
return nil, err
}
-
- if err := d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{
+ if err = d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{
"global": {
"content": [],
"override": [],
@@ -230,7 +230,7 @@ func (d *Database) createAccount(
}`)); err != nil {
return nil, err
}
- return d.accounts.insertAccount(ctx, txn, localpart, hash, appserviceID)
+ return account, nil
}
// SaveAccountData saves new account data for a given user and a given room.

Fix user registration bug (#1777)