Password resets and sending mail

This commit is contained in:
vinchent 2024-08-20 22:20:37 +02:00
parent d256bfb438
commit 6edfc529ea
5 changed files with 67 additions and 0 deletions

View File

@ -405,3 +405,23 @@ func (app *application) VirtualTerminalPaymentSucceeded(w http.ResponseWriter, r
app.writeJSON(w, http.StatusOK, txn)
}
func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Request) {
var payload struct {
Email string `json:"email"`
}
err := app.readJSON(w, r, &payload)
if err != nil {
app.badRequest(w, r, err)
return
}
var data struct {
Link string
}
data.Link = "http://www.vinchent.xyz"
// send mail
}

46
cmd/api/mailer.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"bytes"
"embed"
"fmt"
"text/template"
)
//go:embed templates
var emailTemplateFS embed.FS
func (app *application) SendMail(from, to, subject, tmpl string, data interface{}) error {
templateToRender := fmt.Sprintf("templates/%s.html.tmpl", tmpl)
t, err := template.New("email-html").ParseFS(emailTemplateFS, templateToRender)
if err != nil {
app.errorLog.Println(err)
return err
}
var tpl bytes.Buffer
if err = t.ExecuteTemplate(&tpl, "body", data); err != nil {
app.errorLog.Println(err)
return err
}
formattedMessage := tpl.String()
templateToRender = fmt.Sprintf("templates/%s.plain.tmpl", tmpl)
t, err = template.New("email-plain").ParseFS(emailTemplateFS, templateToRender)
if err != nil {
app.errorLog.Println(err)
return err
}
if err = t.ExecuteTemplate(&tpl, "body", data); err != nil {
app.errorLog.Println(err)
return err
}
plainMessage := tpl.String()
app.infoLog.Println(formattedMessage, plainMessage)
return nil
}

View File

@ -33,6 +33,7 @@ func (app *application) routes() http.Handler {
mux.Post("/virtual-terminal-succeeded", app.VirtualTerminalPaymentSucceeded)
})
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
return mux
}