Add phone registration with SMS verification and regenerate Swagger docs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-13 14:43:33 +05:00
parent 704606b3f5
commit 5e3c46da83
17 changed files with 1008 additions and 20 deletions

View File

@@ -3,11 +3,14 @@ package db
import (
"context"
"errors"
"strings"
"unicode"
"online-back/internal/identity/auth"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
@@ -24,9 +27,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) auth.Reposi
}
const (
loginQuery = `SELECT id, password FROM users WHERE email = $1`
loginQuery = `SELECT id, password FROM users WHERE email = $1`
userByIDQuery = `SELECT id, password FROM users WHERE id = $1`
profileQuery = `SELECT id, name, first_name, last_name, phone, avatar, created_at
profileQuery = `SELECT id, name, first_name, last_name, phone, avatar, created_at
FROM users WHERE id = $1`
)
@@ -81,3 +84,108 @@ func (r *repository) UpdateProfile(ctx context.Context, id int, dto auth.UpdateP
}
return profile, err
}
func (r *repository) UserExists(ctx context.Context, username, phone string) (bool, error) {
const query = `
SELECT EXISTS (
SELECT 1 FROM users
WHERE LOWER(username) = LOWER($1)
OR regexp_replace(phone, '[^0-9]', '', 'g') = $2
)`
var exists bool
err := r.client.QueryRow(ctx, query, username, digitString(phone)).Scan(&exists)
return exists, err
}
func (r *repository) UsernameTaken(ctx context.Context, username, exceptPhone string) (bool, error) {
const query = `
SELECT EXISTS (
SELECT 1 FROM registration_verifications
WHERE LOWER(username) = LOWER($1) AND phone <> $2
)`
var taken bool
err := r.client.QueryRow(ctx, query, username, exceptPhone).Scan(&taken)
return taken, err
}
const upsertVerificationQuery = `
INSERT INTO registration_verifications (name, username, phone, password, code_hash, attempts, expires_at)
VALUES ($1, $2, $3, $4, $5, 0, $6)
ON CONFLICT (phone) DO UPDATE SET
name = EXCLUDED.name,
username = EXCLUDED.username,
password = EXCLUDED.password,
code_hash = EXCLUDED.code_hash,
attempts = 0,
expires_at = EXCLUDED.expires_at,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')`
func (r *repository) UpsertVerification(ctx context.Context, v auth.Verification) error {
_, err := r.client.Exec(ctx, upsertVerificationQuery,
v.Name, v.Username, v.Phone, v.Password, v.CodeHash, v.ExpiresAt,
)
return err
}
const getVerificationQuery = `
SELECT id, name, username, phone, password, code_hash, attempts, expires_at
FROM registration_verifications
WHERE phone = $1`
func (r *repository) GetVerificationByPhone(ctx context.Context, phone string) (auth.Verification, error) {
var v auth.Verification
err := r.client.QueryRow(ctx, getVerificationQuery, phone).Scan(
&v.ID, &v.Name, &v.Username, &v.Phone, &v.Password, &v.CodeHash, &v.Attempts, &v.ExpiresAt,
)
return v, err
}
func (r *repository) IncrementVerifyAttempts(ctx context.Context, id int64) error {
_, err := r.client.Exec(ctx, `
UPDATE registration_verifications
SET attempts = attempts + 1, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $1`, id)
return err
}
func (r *repository) CompleteRegistration(ctx context.Context, v auth.Verification) (int, error) {
tx, err := r.client.Begin(ctx)
if err != nil {
return 0, err
}
defer tx.Rollback(ctx)
var userID int
err = tx.QueryRow(ctx, `
INSERT INTO users (name, username, phone, password, role, active, email)
VALUES ($1, $2, $3, $4, 'user', true, '')
RETURNING id`,
v.Name, v.Username, v.Phone, v.Password,
).Scan(&userID)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
return 0, auth.ErrConflict
}
return 0, err
}
if _, err := tx.Exec(ctx, `DELETE FROM registration_verifications WHERE id = $1`, v.ID); err != nil {
return 0, err
}
if err := tx.Commit(ctx); err != nil {
return 0, err
}
return userID, nil
}
func digitString(s string) string {
var b strings.Builder
for _, r := range s {
if unicode.IsDigit(r) {
b.WriteRune(r)
}
}
return b.String()
}