turash/bugulma/backend/internal/handler/auth_handler.go

134 lines
3.4 KiB
Go

package handler
import (
"net/http"
"strings"
"bugulma/backend/internal/domain"
"bugulma/backend/internal/service"
"github.com/gin-gonic/gin"
)
type AuthHandler struct {
authService *service.AuthService
}
func NewAuthHandler(authService *service.AuthService) *AuthHandler {
return &AuthHandler{authService: authService}
}
type LoginRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
}
type LoginResponse struct {
Token string `json:"token"`
User UserResponse `json:"user"`
}
type UserResponse struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
Role string `json:"role"`
}
func (h *AuthHandler) Login(c *gin.Context) {
var req LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
token, user, err := h.authService.Login(c.Request.Context(), req.Email, req.Password)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
c.JSON(http.StatusOK, LoginResponse{
Token: token,
User: UserResponse{
ID: user.ID,
Email: user.Email,
Name: user.Name,
Role: string(user.Role),
},
})
}
// Me returns the current authenticated user's information
func (h *AuthHandler) Me(c *gin.Context) {
// Get the Authorization header
authHeader := c.GetHeader("Authorization")
if !strings.HasPrefix(authHeader, "Bearer ") {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Missing or invalid authorization header"})
return
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
if tokenString == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Missing token"})
return
}
// Validate the token and get user information
user, err := h.authService.ValidateToken(c.Request.Context(), tokenString)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid or expired token"})
return
}
c.JSON(http.StatusOK, UserResponse{
ID: user.ID,
Email: user.Email,
Name: user.Name,
Role: string(user.Role),
})
}
type RegisterRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required,min=8"`
Name string `json:"name" binding:"required,min=2"`
Role string `json:"role" binding:"required,oneof=user admin content_manager viewer"`
}
func (h *AuthHandler) Register(c *gin.Context) {
var req RegisterRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Convert string role to domain.UserRole
role := domain.UserRole(req.Role)
if role != domain.UserRoleAdmin && role != domain.UserRoleUser &&
role != domain.UserRoleContentManager && role != domain.UserRoleViewer {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role"})
return
}
token, user, err := h.authService.Register(c.Request.Context(), req.Email, req.Password, req.Name, role)
if err != nil {
if err.Error() == "email already registered" {
c.JSON(http.StatusConflict, gin.H{"error": "Email already registered"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create account"})
return
}
c.JSON(http.StatusCreated, LoginResponse{
Token: token,
User: UserResponse{
ID: user.ID,
Email: user.Email,
Name: user.Name,
Role: string(user.Role),
},
})
}