User service: added reset password email endpoints (#272)

This commit is contained in:
Janos Dobronszki
2021-11-16 14:42:24 +00:00
committed by GitHub
parent 92cfa38f25
commit 64773e0e2d
6 changed files with 586 additions and 99 deletions

View File

@@ -31,6 +31,12 @@ type verificationToken struct {
UserID string `json:"userId"`
}
type passwordResetCode struct {
ID string `json:"id"`
Expires time.Time `json:"expires"`
UserID string `json:"userId"`
}
type Domain struct {
db db.DbService
sengridKey string
@@ -59,7 +65,80 @@ func (domain *Domain) SendEmail(fromName, toAddress, toUsername, subject, textCo
}
from := mail.NewEmail(fromName, "support@m3o.com")
to := mail.NewEmail(toUsername, toAddress)
textContent = strings.Replace(textContent, "$micro_verification_link", "https://angry-cori-854281.netlify.app?token="+token+"&redirectUrl="+url.QueryEscape(redirctUrl)+"&failureRedirectUrl="+url.QueryEscape(failureRedirectUrl), -1)
textContent = strings.Replace(textContent, "$micro_verification_link", "https://user.m3o.com?token="+token+"&redirectUrl="+url.QueryEscape(redirctUrl)+"&failureRedirectUrl="+url.QueryEscape(failureRedirectUrl), -1)
message := mail.NewSingleEmail(from, subject, to, textContent, "")
client := sendgrid.NewSendClient(domain.sengridKey)
response, err := client.Send(message)
logger.Info(response)
return err
}
func (domain *Domain) CreatePasswordResetCode(ctx context.Context, userID string) (*passwordResetCode, error) {
pwcode := passwordResetCode{
ID: uuid.New().String(),
Expires: time.Now().Add(24 * time.Hour),
UserID: userID,
}
s := &_struct.Struct{}
jso, _ := json.Marshal(pwcode)
err := s.UnmarshalJSON(jso)
if err != nil {
return nil, err
}
_, err = domain.db.Create(ctx, &db.CreateRequest{
Table: "password-reset-codes",
Record: s,
})
return &pwcode, err
}
func (domain *Domain) DeletePasswordRestCode(ctx context.Context, id string) error {
_, err := domain.db.Delete(ctx, &db.DeleteRequest{
Table: "password-reset-codes",
Id: id,
})
return err
}
// ReadToken returns the user id
func (domain *Domain) ReadPasswordResetCode(ctx context.Context, id string) (*passwordResetCode, error) {
if id == "" {
return nil, errors.New("password reset code id is empty")
}
token := &passwordResetCode{}
rsp, err := domain.db.Read(ctx, &db.ReadRequest{
Table: "password-reset-codes",
Query: fmt.Sprintf("id == '%v'", id),
})
if err != nil {
return nil, err
}
if len(rsp.Records) == 0 {
return nil, errors.New("password reset code not found")
}
m, _ := rsp.Records[0].MarshalJSON()
json.Unmarshal(m, token)
if token.Expires.Before(time.Now()) {
return nil, errors.New("password reset code expired")
}
return token, nil
}
func (domain *Domain) SendPasswordResetEmail(ctx context.Context, userId, fromName, toAddress, toUsername, subject, textContent string) error {
if domain.sengridKey == "" {
return fmt.Errorf("empty email api key")
}
from := mail.NewEmail(fromName, "support@m3o.com")
to := mail.NewEmail(toUsername, toAddress)
code, err := domain.CreatePasswordResetCode(ctx, userId)
if err != nil {
return err
}
textContent = strings.Replace(textContent, "$code", code.ID, -1)
message := mail.NewSingleEmail(from, subject, to, textContent, "")
client := sendgrid.NewSendClient(domain.sengridKey)
response, err := client.Send(message)