feat(auth): implement web login endpoint with JWT

This commit is contained in:
2025-06-16 08:25:20 +03:00
parent 9c7940a70a
commit 45173c406c
5 changed files with 112 additions and 0 deletions

View File

@@ -78,3 +78,31 @@ func (h *AuthHandler) Join(w http.ResponseWriter, r *http.Request) {
// В случае успеха возвращаем пустой ответ со статусом 204
w.WriteHeader(http.StatusNoContent)
}
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
var req models.LoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
token, user, err := h.Service.LoginUser(r.Context(), req)
if err != nil {
if errors.Is(err, core.ErrInvalidCredentials) {
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
log.Printf("internal server error during login: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
response := models.LoginResponse{
Token: token,
User: user,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}