Add password reset functionality to user dashboard page

This commit is contained in:
2024-07-08 23:08:45 +07:00
parent f01407990f
commit 97a9e72d7b
9 changed files with 151 additions and 108 deletions

View File

@ -0,0 +1,45 @@
package userHandlerResetPassword
import (
"github.com/fossyy/filekeeper/cache"
"github.com/fossyy/filekeeper/db"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/types"
"github.com/fossyy/filekeeper/utils"
"net/http"
)
func POST(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
userSession := r.Context().Value("user").(types.User)
currentPassword := r.Form.Get("currentPassword")
password := r.Form.Get("password")
user, err := cache.GetUser(userSession.Email)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
hashPassword, err := utils.HashPassword(password)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !utils.CheckPasswordHash(currentPassword, user.Password) {
http.Redirect(w, r, "/user?error=password_not_match", http.StatusSeeOther)
return
}
err = db.DB.UpdateUserPassword(user.Email, hashPassword)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
session.RemoveAllSessions(userSession.Email)
cache.DeleteUser(userSession.Email)
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}