Skip to content

Commit

Permalink
Merge pull request #22 from Delavalom/ft/add-verify-emails-workflow
Browse files Browse the repository at this point in the history
Ft/add verify emails workflow
  • Loading branch information
Delavalom authored Oct 29, 2023
2 parents 7cbf051 + f0f9f85 commit 62ce202
Show file tree
Hide file tree
Showing 27 changed files with 811 additions and 67 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ migratedown:
migrate -path db/migrations -database "$(DB_SOURCE)" -verbose down

migrateup1:
migrate -path db/migrations -database "$(DB_SOURCE)" -verbose up1
migrate -path db/migrations -database "$(DB_SOURCE)" -verbose up 1
migratedown1:
migrate -path db/migrations -database "$(DB_SOURCE)" -verbose down1
migrate -path db/migrations -database "$(DB_SOURCE)" -verbose down 1

new_migration:
migrate create -ext sql -dir db/migration -seq $(name)
migrate create -ext sql -dir db/migrations -seq $(name)

sqlc:
sqlc generate
Expand Down Expand Up @@ -49,4 +49,4 @@ evans:
redis:
docker run --name redis -p 6379:6379 -d redis:7-alpine

.PHONY: postgres createdb dropdb migrateup migratedown sqlc test server mock migrateup1 migratedown1 proto evans redis
.PHONY: postgres createdb dropdb migrateup migratedown sqlc test server mock migrateup1 migratedown1 proto evans redis new_migration
3 changes: 3 additions & 0 deletions db/migrations/000004_add_verify_emails.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS "verify_emails" CASCADE;

ALTER TABLE "users" DROP COLUMN "is_email_verified";
13 changes: 13 additions & 0 deletions db/migrations/000004_add_verify_emails.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE "verify_emails" (
"id" bigserial PRIMARY KEY,
"username" varchar NOT NULL,
"email" varchar NOT NULL,
"secret_code" varchar NOT NULL,
"is_used" bool NOT NULL DEFAULT false,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"expired_at" timestamptz NOT NULL DEFAULT (now() + interval '15 minutes')
);

ALTER TABLE "verify_emails" ADD FOREIGN KEY ("username") REFERENCES "users" ("username");

ALTER TABLE "users" ADD COLUMN "is_email_verified" bool NOT NULL DEFAULT false;
45 changes: 45 additions & 0 deletions db/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion db/query/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ SET
hashed_password = COALESCE(sqlc.narg(hashed_password), hashed_password),
password_changed_at = COALESCE(sqlc.narg(password_changed_at), password_changed_at),
full_name = COALESCE(sqlc.narg(full_name), full_name),
email = COALESCE(sqlc.narg(email), email)
email = COALESCE(sqlc.narg(email), email),
is_email_verified = COALESCE(sqlc.narg(is_email_verified), is_email_verified)
WHERE
username = sqlc.arg(username)
RETURNING *;
19 changes: 19 additions & 0 deletions db/query/verify_email.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- name: CreateVerifyEmail :one
INSERT INTO verify_emails (
username,
email,
secret_code
) VALUES (
$1, $2, $3
) RETURNING *;

-- name: UpdateVerifyEmail :one
UPDATE verify_emails
SET
is_used = TRUE
WHERE
id = @id
AND secret_code = @secret_code
AND is_used = FALSE
AND expired_at > now()
RETURNING *;
11 changes: 11 additions & 0 deletions db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/sqlc/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Store interface {
Querier
TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error)
CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)
VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error)
}

// SQLStore provides all functions to execute SQL queries and transactions
Expand Down
46 changes: 46 additions & 0 deletions db/sqlc/tx_verify_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package db

import (
"context"

"github.com/jackc/pgx/v5/pgtype"
)

type VerifyEmailTxParams struct {
EmailId int64
SecretCode string
}

type VerifyEmailTxResult struct {
User User
VerifyEmail VerifyEmail
}

func (store *SQLStore) VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error) {
var result VerifyEmailTxResult

err := store.execTx(ctx, func(q *Queries) error {
var err error

result.VerifyEmail, err = q.UpdateVerifyEmail(ctx, UpdateVerifyEmailParams{
ID: arg.EmailId,
SecretCode: arg.SecretCode,
})

if err != nil {
return err
}

result.User, err = q.UpdateUser(ctx, UpdateUserParams{
Username: result.VerifyEmail.Username,
IsEmailVerified: pgtype.Bool{
Bool: true,
Valid: true,
},
})

return err
})

return result, err
}
16 changes: 11 additions & 5 deletions db/sqlc/user.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions db/sqlc/verify_email.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gapi/rpc_create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (server *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest)
if violations != nil {
return nil, invalidArgumentError(violations)
}
hashedPassword, err := util.HashPassword(req.Password)
hashedPassword, err := util.HashPassword(req.GetPassword())
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to hash password: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion gapi/rpc_login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (server *Server) LoginUser(ctx context.Context, req *pb.LoginUserRequest) (
return nil, status.Errorf(codes.Internal, "failed to find user: %s", err)
}

err = util.CheckPassword(req.Password, user.HashedPassword)
err = util.CheckPassword(req.GetPassword(), user.HashedPassword)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "incorrect password: %s", err)
}
Expand Down
Loading

0 comments on commit 62ce202

Please sign in to comment.