added forgot password
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user