feat(auth): add user profile, phone login, and account deletion
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"galo/internal/models"
|
||||
@@ -18,32 +19,36 @@ type SQLiteStore struct {
|
||||
|
||||
func NewSQLiteStore(db *sql.DB) *SQLiteStore { return &SQLiteStore{db: db} }
|
||||
|
||||
const sqliteUserColumns = `id, COALESCE(email, ''), COALESCE(phone, ''), password_hash, name,
|
||||
COALESCE(avatar_url, ''), COALESCE(preferences, '{}'), created_at`
|
||||
|
||||
func (s *SQLiteStore) CreateUser(ctx context.Context, u *models.User) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO users (id, email, password_hash, name, avatar_url)
|
||||
VALUES ($1, $2, $3, $4, $5)`,
|
||||
u.ID, u.Email, u.PasswordHash, u.Name, nullIfEmpty(u.AvatarURL))
|
||||
return err
|
||||
`INSERT INTO users (id, email, phone, password_hash, name, avatar_url)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`,
|
||||
u.ID, nullIfEmpty(u.Email), nullIfEmpty(u.Phone), u.PasswordHash, u.Name, nullIfEmpty(u.AvatarURL))
|
||||
return mapUniqueViolationSQLite(err)
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetUserByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||
return s.scanUser(s.db.QueryRowContext(ctx,
|
||||
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''),
|
||||
COALESCE(preferences, '{}'), created_at
|
||||
FROM users WHERE lower(email) = lower($1)`, email))
|
||||
`SELECT `+sqliteUserColumns+` FROM users WHERE lower(email) = lower($1)`, email))
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetUserByPhone(ctx context.Context, phone string) (*models.User, error) {
|
||||
return s.scanUser(s.db.QueryRowContext(ctx,
|
||||
`SELECT `+sqliteUserColumns+` FROM users WHERE phone = $1`, phone))
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetUserByID(ctx context.Context, id string) (*models.User, error) {
|
||||
return s.scanUser(s.db.QueryRowContext(ctx,
|
||||
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''),
|
||||
COALESCE(preferences, '{}'), created_at
|
||||
FROM users WHERE id = $1`, id))
|
||||
`SELECT `+sqliteUserColumns+` FROM users WHERE id = $1`, id))
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) scanUser(row *sql.Row) (*models.User, error) {
|
||||
var u models.User
|
||||
var prefs, createdAt string
|
||||
if err := row.Scan(&u.ID, &u.Email, &u.PasswordHash, &u.Name, &u.AvatarURL, &prefs, &createdAt); err != nil {
|
||||
if err := row.Scan(&u.ID, &u.Email, &u.Phone, &u.PasswordHash, &u.Name, &u.AvatarURL, &prefs, &createdAt); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
@@ -56,9 +61,34 @@ func (s *SQLiteStore) scanUser(row *sql.Row) (*models.User, error) {
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) UpdateName(ctx context.Context, userID, name string) error {
|
||||
return s.requireRow(s.db.ExecContext(ctx, `UPDATE users SET name = $2 WHERE id = $1`, userID, name))
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) UpdatePasswordHash(ctx context.Context, userID, hash string) error {
|
||||
return s.requireRow(s.db.ExecContext(ctx, `UPDATE users SET password_hash = $2 WHERE id = $1`, userID, hash))
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) AddIdentifier(ctx context.Context, userID, email, phone string) error {
|
||||
err := s.requireRow(s.db.ExecContext(ctx,
|
||||
`UPDATE users
|
||||
SET email = COALESCE(NULLIF($2, ''), email),
|
||||
phone = COALESCE(NULLIF($3, ''), phone)
|
||||
WHERE id = $1`,
|
||||
userID, email, phone))
|
||||
return mapUniqueViolationSQLite(err)
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) DeleteUser(ctx context.Context, userID string) error {
|
||||
return s.requireRow(s.db.ExecContext(ctx, `DELETE FROM users WHERE id = $1`, userID))
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) UpdatePreferences(ctx context.Context, userID string, raw []byte) error {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`UPDATE users SET preferences = $2 WHERE id = $1`, userID, string(raw))
|
||||
return s.requireRow(s.db.ExecContext(ctx,
|
||||
`UPDATE users SET preferences = $2 WHERE id = $1`, userID, string(raw)))
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) requireRow(res sql.Result, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -109,3 +139,21 @@ func (s *SQLiteStore) RevokeAllUserTokens(ctx context.Context, userID string) er
|
||||
userID, sqliteutil.FormatTime(time.Now()))
|
||||
return err
|
||||
}
|
||||
|
||||
// mapUniqueViolationSQLite converts a SQLite unique constraint violation into
|
||||
// the domain sentinel so the service can return a conflict for the right field.
|
||||
func mapUniqueViolationSQLite(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
msg := err.Error()
|
||||
if strings.Contains(msg, "UNIQUE constraint failed") {
|
||||
if strings.Contains(msg, "users.phone") {
|
||||
return ErrPhoneTaken
|
||||
}
|
||||
if strings.Contains(msg, "users.email") {
|
||||
return ErrEmailTaken
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user