Add forgot-password SMS reset, client/staff user lists, and regenerate Swagger.

Fold follow-up migrations into init, expose GET /users/clients and /users/staff, and refresh seed data.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-14 16:42:47 +05:00
parent 5e3c46da83
commit 514b9ca5fc
24 changed files with 1477 additions and 473 deletions

View File

@@ -27,15 +27,35 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) auth.Reposi
}
const (
loginQuery = `SELECT id, password FROM users WHERE email = $1`
loginByNameQuery = `
SELECT id, password FROM users
WHERE LOWER(TRIM(name)) = LOWER(TRIM($1))
OR LOWER(TRIM(username)) = LOWER(TRIM($1))
LIMIT 1`
loginByPhoneQuery = `
SELECT id, password FROM users
WHERE phone = $1
OR ($2 <> '' AND regexp_replace(phone, '[^0-9]', '', 'g') = $2)
LIMIT 1`
userByIDQuery = `SELECT id, password FROM users WHERE id = $1`
profileQuery = `SELECT id, name, first_name, last_name, phone, avatar, created_at
FROM users WHERE id = $1`
)
func (r *repository) Login(ctx context.Context, dto auth.LoginDTO) (auth.UserCredentials, error) {
phone := strings.TrimSpace(dto.Phone)
if phone != "" {
normalized := phone
if n, err := auth.NormalizePhone(phone); err == nil {
normalized = n
}
var result auth.UserCredentials
err := r.client.QueryRow(ctx, loginByPhoneQuery, normalized, digitString(phone)).Scan(&result.Id, &result.Password)
return result, err
}
var result auth.UserCredentials
err := r.client.QueryRow(ctx, loginQuery, dto.Email).Scan(&result.Id, &result.Password)
err := r.client.QueryRow(ctx, loginByNameQuery, strings.TrimSpace(dto.Name)).Scan(&result.Id, &result.Password)
return result, err
}
@@ -180,6 +200,46 @@ func (r *repository) CompleteRegistration(ctx context.Context, v auth.Verificati
return userID, nil
}
func (r *repository) GetUserByName(ctx context.Context, name string) (auth.UserIdentity, error) {
const query = `
SELECT id, name, username, phone FROM users
WHERE LOWER(TRIM(name)) = LOWER(TRIM($1))
LIMIT 1`
var user auth.UserIdentity
err := r.client.QueryRow(ctx, query, name).Scan(&user.Id, &user.Name, &user.Username, &user.Phone)
if errors.Is(err, pgx.ErrNoRows) {
return auth.UserIdentity{}, auth.ErrNotFound
}
return user, err
}
func (r *repository) CompletePasswordReset(ctx context.Context, userID int, verificationID int64, passwordHash string) error {
tx, err := r.client.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
tag, err := tx.Exec(ctx, `
UPDATE users SET
password = $1,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $2`, passwordHash, userID)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return auth.ErrNotFound
}
if _, err := tx.Exec(ctx, `DELETE FROM registration_verifications WHERE id = $1`, verificationID); err != nil {
return err
}
return tx.Commit(ctx)
}
func digitString(s string) string {
var b strings.Builder
for _, r := range s {