Add email confirmation

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-12 12:17:21 +01:00
parent 0e44d39998
commit 1a6e68f13c
11 changed files with 709 additions and 18 deletions

View File

@@ -0,0 +1,2 @@
ALTER TABLE users ADD COLUMN email_address_verified BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE users ALTER COLUMN email_address_verified DROP DEFAULT;

View File

@@ -70,6 +70,7 @@ SELECT
id,
email_address,
hashed_password,
email_address_verified,
fullname,
created_at,
updated_at
@@ -111,6 +112,7 @@ SELECT
id,
email_address,
hashed_password,
email_address_verified,
fullname,
created_at,
updated_at
@@ -148,11 +150,12 @@ func (u *User) Insert(
) error {
q := `
INSERT INTO
users (id, email_address, hashed_password, fullname, created_at, updated_at)
users (id, email_address, hashed_password, email_address_verified, fullname, created_at, updated_at)
VALUES (
@user_id,
@email_address,
@hashed_password,
@email_address_verified,
@fullname,
@created_at,
@updated_at
@@ -160,12 +163,13 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"user_id": u.ID,
"email_address": u.EmailAddress,
"hashed_password": u.HashedPassword,
"fullname": u.FullName,
"created_at": u.CreatedAt,
"updated_at": u.UpdatedAt,
"user_id": u.ID,
"email_address": u.EmailAddress,
"hashed_password": u.HashedPassword,
"fullname": u.FullName,
"created_at": u.CreatedAt,
"updated_at": u.UpdatedAt,
"email_address_verified": u.EmailAddressVerified,
}
_, err := conn.Exec(ctx, q, args)
@@ -185,3 +189,35 @@ VALUES (
return nil
}
func (u *User) UpdateEmailVerification(
ctx context.Context,
conn pg.Conn,
verified bool,
) error {
q := `
UPDATE
users
SET
email_address_verified = @email_address_verified,
updated_at = @updated_at
WHERE
id = @user_id
`
args := pgx.StrictNamedArgs{
"user_id": u.ID,
"email_address_verified": verified,
"updated_at": time.Now(),
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update user email verification: %w", err)
}
u.EmailAddressVerified = verified
u.UpdatedAt = args["updated_at"].(time.Time)
return nil
}