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 1 commit
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
26 changes: 26 additions & 0 deletions internal/usecase/interactor/emails/password_reset_html.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=utf-8
Mime-Version: 1.0

yk-eukarya marked this conversation as resolved.
Show resolved Hide resolved
<!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>
11 changes: 11 additions & 0 deletions internal/usecase/interactor/emails/password_reset_text.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=utf-8
Mime-Version: 1.0
yk-eukarya marked this conversation as resolved.
Show resolved Hide resolved

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!
45 changes: 44 additions & 1 deletion internal/usecase/interactor/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
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"
Expand All @@ -29,9 +33,17 @@ 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
passwordResetHTMLTMPL string
//go:embed emails/password_reset_text.tmpl
passwordResetTextTMPL string
)

func NewUser(r *repo.Container, g *gateway.Container, signupSecret string) interfaces.User {
return &User{
userRepo: r.User,
Expand All @@ -47,6 +59,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 +266,42 @@ 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
}

t1, err := textTmpl.New("passwordReset").Parse(passwordResetTextTMPL)
yk-eukarya marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
t2, err := htmlTmpl.New("passwordReset").Parse(passwordResetHTMLTMPL)
if err != nil {
return err
}
var TextOut, HTMLOut bytes.Buffer
link := "localhost:3000/?pwd-reset-token=" + pr.Token
err = t1.Execute(&TextOut, link)
if err != nil {
return err
}
err = t2.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