tabel user

This commit is contained in:
Yovan
2026-10-03 19:23:22 +07:00
parent 662aaec022
commit 14283c2b61
5 changed files with 75 additions and 8 deletions
+8 -5
View File
@@ -2,6 +2,7 @@ package handler
import (
"encoding/json"
"errors"
"net/http"
"strings"
"time"
@@ -60,7 +61,11 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
}
if err := h.userStore.Create(r.Context(), user); err != nil {
http.Error(w, "Username already exists or create failed", http.StatusBadRequest)
if errors.Is(err, store.ErrUsernameTaken) {
http.Error(w, "Username already exists", http.StatusConflict)
return
}
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
@@ -106,7 +111,7 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(session)
_ = json.NewEncoder(w).Encode(map[string]string{"session_token": session.Token})
}
func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) {
@@ -136,7 +141,5 @@ func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]string{"message": "logged out"})
w.WriteHeader(http.StatusNoContent)
}