Skip to content

Commit

Permalink
[FIX][#4] Send email forgot password using sendgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
jacky-htg committed Jan 17, 2021
1 parent 43def14 commit d429d26
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
22 changes: 18 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ SERVICE_NAME=user-service
SERVICE_VERSION=1+0a
ENV=debug

PORT=9002
APP_NAME=
APP_HOST=
PORT=8000

POSTGRES_HOST='localhost'
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=1234
POSTGRES_DB=user_services
POSTGRES_DB=users

REDIS_ADDRESS=localhost:6379
REDIS_PASSWORD=
REDIS_PASSWORD=

TOKEN_SALT=

CUSTOMERSERVICE_EMAIL=
CUSTOMERSERVICE_PHONE=

SENDGRID_FROM_NAME=
SENDGRID_FROM_EMAIL=
SENDGRID_TEMPLATE_FORGOT_PASSWORD=
SENDGRID_TEMPLATE_REGISTRATION=
SENDGRID_TEMPLATE_NEW_USER=
SENDGRID_API_KEY=
8 changes: 4 additions & 4 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func main() {
// user := users.NewUserServiceClient(conn)
// company := users.NewCompanyServiceClient(conn)
// region := users.NewRegionServiceClient(conn)
branch := users.NewBranchServiceClient(conn)
// branch := users.NewBranchServiceClient(conn)
// employee := users.NewEmployeeServiceClient(conn)
// feature := users.NewFeatureServiceClient(conn)
// packageFeature := users.NewPackageFeatureServiceClient(conn)
// access := users.NewAccessServiceClient(conn)
// group := users.NewGroupServiceClient(conn)

ctx = service.Login(ctx, auth)
// service.ForgotPassword(ctx, auth)
// ctx = service.Login(ctx, auth)
service.ForgotPassword(ctx, auth)
// service.ResetPassword(ctx, auth)
// service.ChangePassword(ctx, auth)
// service.IsAuth(ctx, auth)
Expand All @@ -51,7 +51,7 @@ func main() {
// service.ViewRegion(ctx, region)
// service.DeleteRegion(ctx, region)
// service.ListRegion(ctx, region)
service.CreateBranch(ctx, branch)
// service.CreateBranch(ctx, branch)
// service.UpdateBranch(ctx, branch)
// service.ViewBranch(ctx, branch)
// service.DeleteBranch(ctx, branch)
Expand Down
4 changes: 2 additions & 2 deletions internal/model/request_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ type RequestPassword struct {

// Create func
func (u *RequestPassword) Create(ctx context.Context, db *sql.DB) error {

u.Pb.Id = uuid.New().String()
stmt, err := db.PrepareContext(ctx, `INSERT INTO request_passwords (id, user_id, created_at) VALUES ($1, $2, $3)`)
if err != nil {
return status.Errorf(codes.Internal, "prepare insert: %v", err)
}
defer stmt.Close()

_, err = stmt.ExecContext(ctx, uuid.New().String(), u.Pb.GetUserId(), time.Now().UTC())
_, err = stmt.ExecContext(ctx, u.Pb.GetId(), u.Pb.GetUserId(), time.Now().UTC())
if err != nil {
return status.Errorf(codes.Internal, "exec insert: %v", err)
}
Expand Down
23 changes: 23 additions & 0 deletions internal/pkg/email/email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package email

import (
"os"

"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)

// SendMailV3 for send mail from sendgrid
func SendMailV3(from *mail.Email, p *mail.Personalization, templateID string) error {
m := mail.NewV3Mail()
m.SetFrom(from)
m.SetTemplateID(templateID)
m.AddPersonalizations(p)

request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
_, err := sendgrid.API(request)

return err
}
22 changes: 21 additions & 1 deletion internal/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package service
import (
"context"
"database/sql"
"os"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"user-service/internal/model"
"user-service/internal/pkg/app"
"user-service/internal/pkg/db/redis"
"user-service/internal/pkg/email"
"user-service/internal/pkg/token"
users "user-service/pb"
)
Expand Down Expand Up @@ -72,7 +75,24 @@ func (u *Auth) ForgotPassword(ctx context.Context, in *users.ForgotPasswordReque
return &output, err
}

// TODO : send email change password
// send email link forgot password
from := mail.NewEmail(os.Getenv("SENDGRID_FROM_NAME"), os.Getenv("SENDGRID_FROM_EMAIL"))
p := mail.NewPersonalization()
tos := []*mail.Email{
mail.NewEmail(userModel.Pb.GetName(), userModel.Pb.GetEmail()),
}
p.AddTos(tos...)

p.SetDynamicTemplateData("name", userModel.Pb.GetName())
p.SetDynamicTemplateData("url", os.Getenv("APP_HOST")+"/change-password/"+requestPasswordModel.Pb.GetId())
p.SetDynamicTemplateData("app_name", os.Getenv("APP_NAME"))
p.SetDynamicTemplateData("cs_email", os.Getenv("CUSTOMERSERVICE_EMAIL"))
p.SetDynamicTemplateData("cs_phone", os.Getenv("CUSTOMERSERVICE_PHONE"))

err = email.SendMailV3(from, p, os.Getenv("SENDGRID_TEMPLATE_FORGOT_PASSWORD"))
if err != nil {
return &output, status.Errorf(codes.Internal, "send link forgot password: %v", err)
}

output.Message = "Success"
return &output, nil
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"user-service/internal/route"
)

const defaultPort = "9001"
const defaultPort = "8000"

func main() {
// lookup and setup env
Expand Down

0 comments on commit d429d26

Please sign in to comment.