mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
147 lines
3.6 KiB
Go
147 lines
3.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"novamd/internal/auth"
|
|
"novamd/internal/httpcontext"
|
|
"novamd/internal/models"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type LoginRequest struct {
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
type LoginResponse struct {
|
|
AccessToken string `json:"accessToken"`
|
|
RefreshToken string `json:"refreshToken"`
|
|
User *models.User `json:"user"`
|
|
Session *auth.Session `json:"session"`
|
|
}
|
|
|
|
type RefreshRequest struct {
|
|
RefreshToken string `json:"refreshToken"`
|
|
}
|
|
|
|
type RefreshResponse struct {
|
|
AccessToken string `json:"accessToken"`
|
|
}
|
|
|
|
// Login handles user authentication and returns JWT tokens
|
|
func (h *Handler) Login(authService *auth.SessionService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req LoginRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Validate request
|
|
if req.Email == "" || req.Password == "" {
|
|
http.Error(w, "Email and password are required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Get user from database
|
|
user, err := h.DB.GetUserByEmail(req.Email)
|
|
if err != nil {
|
|
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
// Verify password
|
|
err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(req.Password))
|
|
if err != nil {
|
|
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
// Create session and generate tokens
|
|
session, accessToken, err := authService.CreateSession(user.ID, string(user.Role))
|
|
if err != nil {
|
|
http.Error(w, "Failed to create session", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Prepare response
|
|
response := LoginResponse{
|
|
AccessToken: accessToken,
|
|
RefreshToken: session.RefreshToken,
|
|
User: user,
|
|
Session: session,
|
|
}
|
|
|
|
respondJSON(w, response)
|
|
}
|
|
}
|
|
|
|
// Logout invalidates the user's session
|
|
func (h *Handler) Logout(authService *auth.SessionService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
sessionID := r.Header.Get("X-Session-ID")
|
|
if sessionID == "" {
|
|
http.Error(w, "Session ID required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
err := authService.InvalidateSession(sessionID)
|
|
if err != nil {
|
|
http.Error(w, "Failed to logout", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
// RefreshToken generates a new access token using a refresh token
|
|
func (h *Handler) RefreshToken(authService *auth.SessionService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req RefreshRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if req.RefreshToken == "" {
|
|
http.Error(w, "Refresh token required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Generate new access token
|
|
accessToken, err := authService.RefreshSession(req.RefreshToken)
|
|
if err != nil {
|
|
http.Error(w, "Invalid refresh token", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
response := RefreshResponse{
|
|
AccessToken: accessToken,
|
|
}
|
|
|
|
respondJSON(w, response)
|
|
}
|
|
}
|
|
|
|
// GetCurrentUser returns the currently authenticated user
|
|
func (h *Handler) GetCurrentUser() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
ctx, ok := httpcontext.GetRequestContext(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// Get user from database
|
|
user, err := h.DB.GetUserByID(ctx.UserID)
|
|
if err != nil {
|
|
http.Error(w, "User not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
respondJSON(w, user)
|
|
}
|
|
}
|