diff --git a/handlers/user_management.go b/handlers/user_management.go new file mode 100644 index 0000000..9a0a547 --- /dev/null +++ b/handlers/user_management.go @@ -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 + } +} diff --git a/routes/routes.go b/routes/routes.go index c314c4f..631858c 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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") diff --git a/services/user_management.go b/services/user_management.go new file mode 100644 index 0000000..f886a8f --- /dev/null +++ b/services/user_management.go @@ -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 +}