Password resets and sending mail
This commit is contained in:
		@ -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
									
								
							
							
						
						
									
										46
									
								
								cmd/api/mailer.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										0
									
								
								cmd/api/templates/password-reset.html.gohtml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								cmd/api/templates/password-reset.html.gohtml
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										0
									
								
								cmd/api/templates/password-reset.plain.gohtml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								cmd/api/templates/password-reset.plain.gohtml
									
									
									
									
									
										Normal file
									
								
							
		Reference in New Issue
	
	Block a user