feat(auth): add email handling for user authentication and account setup
feat(api): update API methods to include email and identifier for authentication feat(ui): enhance authentication forms to support email input feat(settings): implement language selection in settings dialog feat(language): add language management for multilingual support
This commit is contained in:
1 parent
cac2faae2e
commit
6394374766
9 files changed
+212
-55
No files matched your search
+19
-15
@@ -117,10 +117,13 @@ func (d *DB) initSchema(ctx context.Context) error {
|
||||
CREATE TABLE IF NOT EXISTS auth_users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
email TEXT NOT NULL DEFAULT '',
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS email TEXT NOT NULL DEFAULT '';
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS auth_users_email_idx ON auth_users (lower(email)) WHERE email <> '';
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS auth_users_singleton_idx ON auth_users ((true));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS auth_sessions (
|
||||
@@ -186,6 +189,7 @@ func (d *DB) cleanupNotifications(ctx context.Context) {
|
||||
type AuthUser struct {
|
||||
ID int64
|
||||
Username string
|
||||
Email string
|
||||
PasswordHash string
|
||||
}
|
||||
|
||||
@@ -199,13 +203,13 @@ func (d *DB) countUsers(ctx context.Context) (int, error) {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (d *DB) createUser(ctx context.Context, username, passwordHash string) (*AuthUser, error) {
|
||||
func (d *DB) createUser(ctx context.Context, username, email, passwordHash string) (*AuthUser, error) {
|
||||
var user AuthUser
|
||||
err := d.pool.QueryRow(ctx, `
|
||||
INSERT INTO auth_users (username, password_hash, updated_at)
|
||||
VALUES ($1, $2, now())
|
||||
RETURNING id, username
|
||||
`, username, passwordHash).Scan(&user.ID, &user.Username)
|
||||
INSERT INTO auth_users (username, email, password_hash, updated_at)
|
||||
VALUES ($1, $2, $3, now())
|
||||
RETURNING id, username, email
|
||||
`, username, email, passwordHash).Scan(&user.ID, &user.Username, &user.Email)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
@@ -217,13 +221,13 @@ func (d *DB) createUser(ctx context.Context, username, passwordHash string) (*Au
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (d *DB) getUserByUsername(ctx context.Context, username string) (*AuthUser, error) {
|
||||
func (d *DB) getUserByIdentifier(ctx context.Context, identifier string) (*AuthUser, error) {
|
||||
var user AuthUser
|
||||
err := d.pool.QueryRow(ctx, `
|
||||
SELECT id, username, password_hash
|
||||
SELECT id, username, email, password_hash
|
||||
FROM auth_users
|
||||
WHERE username = $1
|
||||
`, username).Scan(&user.ID, &user.Username, &user.PasswordHash)
|
||||
WHERE username = $1 OR lower(email) = lower($1)
|
||||
`, identifier).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -236,11 +240,11 @@ func (d *DB) getUserByUsername(ctx context.Context, username string) (*AuthUser,
|
||||
func (d *DB) getSessionUser(ctx context.Context, tokenHash string) (*AuthUser, error) {
|
||||
var user AuthUser
|
||||
err := d.pool.QueryRow(ctx, `
|
||||
SELECT u.id, u.username, u.password_hash
|
||||
SELECT u.id, u.username, u.email, u.password_hash
|
||||
FROM auth_sessions s
|
||||
JOIN auth_users u ON u.id = s.user_id
|
||||
WHERE s.token_hash = $1 AND s.expires_at > now()
|
||||
`, tokenHash).Scan(&user.ID, &user.Username, &user.PasswordHash)
|
||||
`, tokenHash).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -269,12 +273,12 @@ func (d *DB) deleteSession(ctx context.Context, tokenHash string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DB) updateUserCredentials(ctx context.Context, userID int64, username, passwordHash string) error {
|
||||
func (d *DB) updateUserCredentials(ctx context.Context, userID int64, username, email, passwordHash string) error {
|
||||
_, err := d.pool.Exec(ctx, `
|
||||
UPDATE auth_users
|
||||
SET username = $1, password_hash = $2, updated_at = now()
|
||||
WHERE id = $3
|
||||
`, username, passwordHash, userID)
|
||||
SET username = $1, email = $2, password_hash = $3, updated_at = now()
|
||||
WHERE id = $4
|
||||
`, username, email, passwordHash, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update user credentials: %w", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user