Add a method to validate email
All checks were successful
ci/woodpecker/push/build Pipeline was successful

Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
This commit is contained in:
2026-06-10 13:58:02 +02:00
parent 3ea6765486
commit 6e5e0fc805
5 changed files with 28 additions and 7 deletions

View File

@@ -105,3 +105,12 @@ func (a *PublicAccountService) SignUp(ctx context.Context, in *accounts.SignUpRe
},
}, nil
}
// Check if an email is already used by an account
func (a *PublicAccountService) IsEmailAvailable(ctx context.Context, in *accounts.IsEmailUsedRequest) (*accounts.IsEmailUsedResponse, error) {
exists, err := a.accountsCtrl.IsExist(ctx, in.GetEmail())
if err != nil {
return nil, status.Error(codes.Aborted, "Couldn't check email availability")
}
return &accounts.IsEmailUsedResponse{Used: exists}, nil
}

View File

@@ -64,15 +64,15 @@ func GetUUIDForEmail(ctx context.Context, db *sql.DB, email string) (uuid string
return
}
// IsAccountExist checks if an account with a UUID exists in the db
func IsAccountExist(ctx context.Context, db *sql.DB, uuid string) (bool, error) {
// IsAccountExist checks if an account with an email exists in the db
func IsAccountExist(ctx context.Context, db *sql.DB, email string) (bool, error) {
exists := false
err := db.QueryRowContext(
ctx,
`SELECT EXISTS(
SELECT 1 FROM accounts WHERE uuid = $1
SELECT 1 FROM accounts WHERE email = $1
)`,
uuid,
email,
).Scan(&exists)
return exists, err
}

View File

@@ -105,3 +105,15 @@ func (c *AccountController) Login(ctx context.Context, email, password string) (
return uuid, nil
}
func (c *AccountController) IsExist(ctx context.Context, email string) (bool, error) {
log := logger.FromContext(ctx)
log.V(2).Info("Checking if an account with email exists")
exists, err := repository.IsAccountExist(ctx, c.DB, email)
if err != nil {
return false, ErrServerError
}
return exists, nil
}