From 0c982c8f4773f1f9096cd24c72bf701ff6b688ea Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Tue, 13 Jan 2026 12:38:46 +0100 Subject: [PATCH] fix duplicate accounts --- .gitignore | 3 ++- grpcapi/grpcapi.go | 4 ++-- handlers/accounts.go | 10 ++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index f702e7b..da7de7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config.yaml .vscode -__debug_bin \ No newline at end of file +__debug_bin +mobility-accounts diff --git a/grpcapi/grpcapi.go b/grpcapi/grpcapi.go index b6973b0..e4d0222 100755 --- a/grpcapi/grpcapi.go +++ b/grpcapi/grpcapi.go @@ -78,7 +78,7 @@ func (s MobilityAccountsServerImpl) GetAccount(ctx context.Context, req *GetAcco account, err := s.handler.GetAccount(req.Id) if err != nil { log.Error().Err(err).Msg("") - return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err) + return nil, status.Errorf(codes.NotFound, "issue while retrieving account : %v", err) } response, err := AccountFromStorageType(account) if err != nil { @@ -91,7 +91,7 @@ func (s MobilityAccountsServerImpl) GetAccountUsername(ctx context.Context, req account, err := s.handler.GetAccountUsername(req.Username, req.Namespace) if err != nil { log.Error().Err(err).Msg("") - return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err) + return nil, status.Errorf(codes.NotFound, "issue while retrieving account : %v", err) } response, err := AccountFromStorageType(account) if err != nil { diff --git a/handlers/accounts.go b/handlers/accounts.go index 822f1c7..06edf98 100755 --- a/handlers/accounts.go +++ b/handlers/accounts.go @@ -49,8 +49,13 @@ func (h MobilityAccountsHandler) Register(account storage.Account) (*storage.Acc account.Authentication.Local.Username = &email } - //TODO remove this as we want to handle unicity in storage if account.Authentication.Local != nil { + // Check if account with same username/email/phone already exists + existing, _ := h.storage.DB.LocalAuthentication(account.Namespace, account.Authentication.Local.Username, account.Authentication.Local.Email, account.Authentication.Local.PhoneNumber) + if existing != nil { + return nil, errors.New("account with same credentials already exists") + } + // If a password was sent, hash the password if account.Authentication.Local.Password != "" { hashedPassword, err := bcrypt.GenerateFromPassword([]byte(account.Authentication.Local.Password), bcrypt.DefaultCost) @@ -59,9 +64,6 @@ func (h MobilityAccountsHandler) Register(account storage.Account) (*storage.Acc } account.Authentication.Local.Password = string(hashedPassword) } - - _, _ = h.storage.DB.LocalAuthentication(account.Namespace, account.Authentication.Local.Username, account.Authentication.Local.Email, account.Authentication.Local.PhoneNumber) - } // Validate data schemas