summaryrefslogtreecommitdiff
path: root/handlers/user.go
diff options
context:
space:
mode:
authorhaturatu <taro@eyes4you.org>2025-10-12 17:04:07 +0900
committerhaturatu <taro@eyes4you.org>2025-10-12 17:04:07 +0900
commit957dc75eddf69ccf26bd387828b9e205a5425f30 (patch)
tree15a3f5581dded804a383c1d42e5b7d4b967da11e /handlers/user.go
parent10c90d8654b94916bf0a4ffb8263e512a8e6be04 (diff)
add(api): api endpoint authorized Bearer token
Diffstat (limited to 'handlers/user.go')
-rw-r--r--handlers/user.go56
1 files changed, 41 insertions, 15 deletions
diff --git a/handlers/user.go b/handlers/user.go
index 196405b..d55f93d 100644
--- a/handlers/user.go
+++ b/handlers/user.go
@@ -2,6 +2,7 @@ package handlers
import (
"auth-proxy/config"
+ "auth-proxy/logging"
"auth-proxy/models"
"auth-proxy/types"
"auth-proxy/utils"
@@ -11,7 +12,7 @@ import (
// AccountPageHandler serves the account page, passing user data to the template.
func AccountPageHandler(w http.ResponseWriter, r *http.Request) {
- user, ok := r.Context().Value(types.SessionDataContextKey).(*models.User)
+ user, ok := r.Context().Value(types.SessionDataContextKey).(*types.User)
if !ok || user == nil {
// This should ideally not happen if AuthMiddleware is working correctly.
http.Error(w, "User not authenticated", http.StatusUnauthorized)
@@ -22,7 +23,9 @@ func AccountPageHandler(w http.ResponseWriter, r *http.Request) {
"Paths": config.Paths,
"User": user,
}
- templates.ExecuteTemplate(w, "account.html", data)
+ if err := templates.ExecuteTemplate(w, "account.html", data); err != nil {
+ logging.AppLog.Error("Failed to execute account template", "error", err, "user_id", user.ID)
+ }
}
// ChangePasswordHandler handles both displaying the page (GET) and processing the update (POST).
@@ -32,69 +35,92 @@ func ChangePasswordHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"Paths": config.Paths,
}
- templates.ExecuteTemplate(w, "account_password.html", data)
+ if err := templates.ExecuteTemplate(w, "account_password.html", data); err != nil {
+ logging.AppLog.Error("Failed to execute account_password template", "error", err)
+ }
return
}
// POST request logic (API call)
if r.Method == http.MethodPost {
// This is a protected route, so AuthMiddleware should have put the user in the context.
- user, ok := r.Context().Value(types.SessionDataContextKey).(*models.User)
+ user, ok := r.Context().Value(types.SessionDataContextKey).(*types.User)
if !ok || user == nil {
- http.Error(w, "User not authenticated", http.StatusUnauthorized)
- return
+ http.Error(w, "User not authenticated", http.StatusUnauthorized)
+ return
}
w.Header().Set("Content-Type", "application/json")
- r.ParseForm()
+ if err := r.ParseForm(); err != nil {
+ logging.AppLog.Error("Failed to parse change password form", "error", err)
+ w.WriteHeader(http.StatusBadRequest)
+ if err := json.NewEncoder(w).Encode(map[string]string{"error": "Could not read request."}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
+ return
+ }
currentPassword := r.FormValue("current_password")
newPassword := r.FormValue("new_password")
confirmNewPassword := r.FormValue("confirm_new_password")
if ok, message := utils.ValidatePassword(newPassword); !ok {
w.WriteHeader(http.StatusBadRequest)
- json.NewEncoder(w).Encode(map[string]string{"error": message})
+ if err := json.NewEncoder(w).Encode(map[string]string{"error": message}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
return
}
if newPassword != confirmNewPassword {
w.WriteHeader(http.StatusBadRequest)
- json.NewEncoder(w).Encode(map[string]string{"error": "New passwords do not match."})
+ if err := json.NewEncoder(w).Encode(map[string]string{"error": "New passwords do not match."}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
return
}
dbUser, err := models.GetUserByID(user.ID)
if err != nil || dbUser == nil {
w.WriteHeader(http.StatusInternalServerError)
- json.NewEncoder(w).Encode(map[string]string{"error": "Could not retrieve user data."})
+ if err := json.NewEncoder(w).Encode(map[string]string{"error": "Could not retrieve user data."}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
return
}
if !models.CheckPasswordHash(currentPassword, dbUser.PasswordHash) {
w.WriteHeader(http.StatusUnauthorized)
- json.NewEncoder(w).Encode(map[string]string{"error": "Current password is incorrect."})
+ if err := json.NewEncoder(w).Encode(map[string]string{"error": "Current password is incorrect."}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
return
}
newPasswordHash, err := models.HashPassword(newPassword)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
- json.NewEncoder(w).Encode(map[string]string{"error": "Failed to process new password."})
+ if err := json.NewEncoder(w).Encode(map[string]string{"error": "Failed to process new password."}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
return
}
if err := models.UpdateUserPassword(dbUser.ID, newPasswordHash); err != nil {
w.WriteHeader(http.StatusInternalServerError)
- json.NewEncoder(w).Encode(map[string]string{"error": "Failed to update password."})
+ if err := json.NewEncoder(w).Encode(map[string]string{"error": "Failed to update password."}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
return
}
w.WriteHeader(http.StatusOK)
- json.NewEncoder(w).Encode(map[string]string{"message": "Password updated successfully."})
+ if err := json.NewEncoder(w).Encode(map[string]string{"message": "Password updated successfully."}); err != nil {
+ logging.AppLog.Error("Failed to encode json error response", "error", err)
+ }
return
}
// Handle other methods
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
-} \ No newline at end of file
+}