Creating backend to handler password resets

This commit is contained in:
2024-08-21 12:54:25 +02:00
parent 333499f76e
commit b98b61aa76
8 changed files with 77 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/stripe/stripe-go/v79"
"golang.org/x/crypto/bcrypt"
)
type stripePayload struct {
@ -465,3 +466,45 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re
app.writeJSON(w, http.StatusCreated, resp)
}
func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) {
var payload struct {
Email string `json:"email"`
Password string `json:"password"`
}
err := app.readJSON(w, r, &payload)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
user, err := app.DB.GetUserByEmail(payload.Email)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
newHash, err := bcrypt.GenerateFromPassword([]byte(payload.Password), 12)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
err = app.DB.UpdatePasswordForUser(user, string(newHash))
if err != nil {
app.badRequest(w, r, err)
return
}
var resp struct {
Error bool `json:"error"`
Message string `json:"message"`
}
resp.Error = false
resp.Message = "Password reset."
app.writeJSON(w, http.StatusCreated, resp)
}

View File

@ -27,13 +27,14 @@ func (app *application) routes() http.Handler {
mux.Route("/api/admin", func(mux chi.Router) {
mux.Use(app.Auth)
mux.Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("got in"))
})
// mux.Get("/test", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("got in"))
// })
mux.Post("/virtual-terminal-succeeded", app.VirtualTerminalPaymentSucceeded)
})
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
mux.Post("/api/reset-password", app.ResetPassword)
return mux
}

View File

@ -12,6 +12,7 @@
<p>
<a href="{{.Link}}">{{.Link}}</a>
</p>
<p>This link expires in 10 minutes.</p>
<p>
--
<br>

View File

@ -7,6 +7,8 @@ Click on the link below to get started:
{{.Link}}
This link expires in 10 minutes.
--
Widget Co.
{{ end }}