Using urlsigner package

This commit is contained in:
vinchent 2024-08-21 11:44:38 +02:00
parent 7a7a823715
commit 7db64eff6a
2 changed files with 24 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"myapp/internal/cards" "myapp/internal/cards"
"myapp/internal/models" "myapp/internal/models"
"myapp/internal/urlsigner"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -417,16 +418,36 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re
return return
} }
// verify that email exists
_, err = app.DB.GetUserByEmail(payload.Email)
if err != nil {
var resp struct {
Error bool `json:"error"`
Message string `json:"message"`
}
resp.Error = true
resp.Message = "No matching email found on our system"
app.writeJSON(w, http.StatusAccepted, resp)
return
}
link := fmt.Sprintf("%s/reset-password?email=%s", app.config.frontend, payload.Email)
sign := urlsigner.Signer{
Secret: []byte(app.config.secretkey),
}
signedLink := sign.GenerateTokenFromString(link)
var data struct { var data struct {
Link string Link string
} }
data.Link = "http://www.vinchent.xyz" data.Link = signedLink
// send mail // send mail
err = app.SendMail( err = app.SendMail(
"info@widgets.com", "info@widgets.com",
"info@widgets.com", payload.Email,
"Password Reset Request", "Password Reset Request",
"password-reset", "password-reset",
data, data,

View File

@ -32,6 +32,7 @@ func (s *Signer) VerifyToken(token string) bool {
_, err := crypt.Unsign([]byte(token)) _, err := crypt.Unsign([]byte(token))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return false
} }
return true return true
} }