Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

feat: send mail #86

Merged
merged 3 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/usecase/interactor/emails/password_reset_html.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
</head>

<body>

<h1>ReEarth</h1>
<p>
You have submitted a password change request! If it was you, please confirm the password change.
To reset your password click on the following link:
</p>

<a href="{{ . }}">{{ . }}</a>

<p>If you are having any issues with your account, please don't hesitate to contact us by replying to this mail. Thank you!</p>

</body>

</html>
7 changes: 7 additions & 0 deletions internal/usecase/interactor/emails/password_reset_text.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ReEarth
You have submitted a password change request! If it was you, please confirm the password change.
To reset your password click on the following link:

{{ . }}

If you are having any issues with your account, please don't hesitate to contact us by replying to this mail. Thank you!
53 changes: 52 additions & 1 deletion internal/usecase/interactor/user.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package interactor

import (
"bytes"
"context"
_ "embed"
"errors"
htmlTmpl "html/template"
"net/mail"
textTmpl "text/template"

"github.com/reearth/reearth-backend/internal/usecase"
"github.com/reearth/reearth-backend/internal/usecase/gateway"
"github.com/reearth/reearth-backend/internal/usecase/interfaces"
"github.com/reearth/reearth-backend/internal/usecase/repo"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/log"
"github.com/reearth/reearth-backend/pkg/project"
"github.com/reearth/reearth-backend/pkg/rerror"
"github.com/reearth/reearth-backend/pkg/user"
Expand All @@ -29,9 +34,32 @@ type User struct {
transaction repo.Transaction
file gateway.File
authenticator gateway.Authenticator
mailer gateway.Mailer
signupSecret string
}

var (
//go:embed emails/password_reset_html.tmpl
passwordResetHTMLTMPLStr string
//go:embed emails/password_reset_text.tmpl
passwordResetTextTMPLStr string

passwordResetTextTMPL *textTmpl.Template
passwordResetHTMLTMPL *htmlTmpl.Template
)

func init() {
var err error
passwordResetTextTMPL, err = textTmpl.New("passwordReset").Parse(passwordResetTextTMPLStr)
if err != nil {
log.Panicf("password reset email template parse error: %s", err)
yk-eukarya marked this conversation as resolved.
Show resolved Hide resolved
}
passwordResetHTMLTMPL, err = htmlTmpl.New("passwordReset").Parse(passwordResetHTMLTMPLStr)
if err != nil {
log.Panicf("password reset email template parse error: %s", err)
yk-eukarya marked this conversation as resolved.
Show resolved Hide resolved
}
}

func NewUser(r *repo.Container, g *gateway.Container, signupSecret string) interfaces.User {
return &User{
userRepo: r.User,
Expand All @@ -47,6 +75,7 @@ func NewUser(r *repo.Container, g *gateway.Container, signupSecret string) inter
file: g.File,
authenticator: g.Authenticator,
signupSecret: signupSecret,
mailer: g.Mailer,
}
}

Expand Down Expand Up @@ -253,12 +282,34 @@ func (i *User) StartPasswordReset(ctx context.Context, email string) error {
return err
}

u.SetPasswordReset(user.NewPasswordReset())
pr := user.NewPasswordReset()
u.SetPasswordReset(pr)

if err := i.userRepo.Save(ctx, u); err != nil {
return err
}

var TextOut, HTMLOut bytes.Buffer
link := "localhost:3000/?pwd-reset-token=" + pr.Token
err = passwordResetTextTMPL.Execute(&TextOut, link)
if err != nil {
return err
}
err = passwordResetHTMLTMPL.Execute(&HTMLOut, link)
if err != nil {
return err
}

err = i.mailer.SendMail([]gateway.Contact{
{
Email: u.Email(),
Name: u.Name(),
},
}, "Password reset", TextOut.String(), HTMLOut.String())
if err != nil {
return err
}

tx.Commit()
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ func Errorln(args ...interface{}) {
func Fatalln(args ...interface{}) {
logrus.Fatalln(args...)
}

func Panicf(format string, args ...interface{}) {
logrus.Panicf(format, args...)
}