added forgot password

This commit is contained in:
2026-01-21 11:12:46 +08:00
parent 18c845ddc8
commit 7caf9b069d
3 changed files with 45 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package handlers
import (
"authentication/helper"
"authentication/services"
"net/http"
)
func ForgotPassword(w http.ResponseWriter, r *http.Request) {
email, err := helper.ExtractEmailFromToken(r.Header.Get("Authorization"))
if err != nil {
helper.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
allowed, err := services.ForgotPassword(email)
if err != nil {
helper.RespondWithError(w, http.StatusBadGateway, "Failed to process forgot password request")
return
}
if !allowed {
helper.RespondWithError(w, http.StatusForbidden, "Password reset not allowed for this email")
return
}
}
+1
View File
@@ -18,6 +18,7 @@ func SetupRoutes(router *mux.Router, db *sql.DB) {
authRoutes.HandleFunc("/callback", handlers.GoogleCallback).Methods("GET")
authRoutes.HandleFunc("/refresh_token", handlers.HandleTokenRefresh).Methods("GET", "POST", "OPTIONS")
authRoutes.HandleFunc("/logout", handlers.LogoutHandler).Methods("GET")
authRoutes.HandleFunc("/forgot-password", handlers.ForgotPassword).Methods("GET")
// authRoutes.HandleFunc("/microsoft/login", handlers.MicrosoftLogin).Methods("GET")
// authRoutes.HandleFunc("/microsoft/callback", handlers.MicrosotCallback).Methods("GET")
+17
View File
@@ -0,0 +1,17 @@
package services
import "authentication/db"
func ForgotPassword(email string) (bool, error) {
selectQuery := "SELECT EXISTS(SELECT 1 FROM uess_user_management.users WHERE email_address = ?)"
var exists string
err := db.DB.QueryRow(selectQuery, email).Scan(&exists)
if err != nil {
return false, err
}
if exists == "0" {
return false, nil
}
return true, nil
}