Skip to content

Commit

Permalink
feat(email): Add API to verify email
Browse files Browse the repository at this point in the history
  • Loading branch information
eizyc committed Jun 29, 2024
1 parent c3b2d94 commit 91a1829
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 59 deletions.
2 changes: 1 addition & 1 deletion app.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ REDIS_ADDRESS=0.0.0.0:6379
EMAIL_SENDER_NAME=Simple Bank
EMAIL_SENDER_ADDRESS=[email protected]
EMAIL_SENDER_PASSWORD=prgctnuzmwqlkcrn
EMAIL_VERIFY_ADDRESS=https://localhost:8080/v1/verify_email
EMAIL_VERIFY_ADDRESS=http://localhost:8080/v1/verify_email
15 changes: 15 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 *;
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
44 changes: 44 additions & 0 deletions db/sqlc/tx_verify_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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
}
7 changes: 5 additions & 2 deletions db/sqlc/user.sql.go

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

47 changes: 47 additions & 0 deletions doc/swagger/simple_bank.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,45 @@
"SimpleBank"
]
}
},
"/v1/verify_email": {
"get": {
"summary": "Verify email",
"description": "Use this API to verify user's email address",
"operationId": "SimpleBank_VerifyEmail",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/pbVerifyEmailResponse"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "emailId",
"in": "query",
"required": false,
"type": "string",
"format": "int64"
},
{
"name": "secretCode",
"in": "query",
"required": false,
"type": "string"
}
],
"tags": [
"SimpleBank"
]
}
}
},
"definitions": {
Expand Down Expand Up @@ -233,6 +272,14 @@
}
}
},
"pbVerifyEmailResponse": {
"type": "object",
"properties": {
"isVerified": {
"type": "boolean"
}
}
},
"protobufAny": {
"type": "object",
"properties": {
Expand Down
44 changes: 44 additions & 0 deletions gapi/rpc_verify_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package gapi

import (
"context"

db "github.com/eizyc/simplebank/db/sqlc"
"github.com/eizyc/simplebank/pb"
"github.com/eizyc/simplebank/val"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (server *Server) VerifyEmail(ctx context.Context, req *pb.VerifyEmailRequest) (*pb.VerifyEmailResponse, error) {
violations := validateVerifyEmailRequest(req)
if violations != nil {
return nil, invalidArgumentError(violations)
}

txResult, err := server.store.VerifyEmailTx(ctx, db.VerifyEmailTxParams{
EmailId: req.GetEmailId(),
SecretCode: req.GetSecretCode(),
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to verify email")
}

rsp := &pb.VerifyEmailResponse{
IsVerified: txResult.User.IsEmailVerified,
}
return rsp, nil
}

func validateVerifyEmailRequest(req *pb.VerifyEmailRequest) (violations []*errdetails.BadRequest_FieldViolation) {
if err := val.ValidateEmailId(req.GetEmailId()); err != nil {
violations = append(violations, fieldViolation("email_id", err))
}

if err := val.ValidateSecretCode(req.GetSecretCode()); err != nil {
violations = append(violations, fieldViolation("secret_code", err))
}

return violations
}
Loading

0 comments on commit 91a1829

Please sign in to comment.