| package handler
|
|
|
| import (
|
| "net/http"
|
| "opus-api/internal/middleware"
|
| "opus-api/internal/service"
|
| "opus-api/internal/types"
|
|
|
| "github.com/gin-gonic/gin"
|
| )
|
|
|
|
|
| type AuthHandler struct {
|
| authService *service.AuthService
|
| }
|
|
|
|
|
| func NewAuthHandler(authService *service.AuthService) *AuthHandler {
|
| return &AuthHandler{authService: authService}
|
| }
|
|
|
|
|
| type LoginRequest struct {
|
| Username string `json:"username" binding:"required"`
|
| Password string `json:"password" binding:"required"`
|
| }
|
|
|
|
|
| type LoginResponse struct {
|
| Token string `json:"token"`
|
| User User `json:"user"`
|
| }
|
|
|
|
|
| type User struct {
|
| ID uint `json:"id"`
|
| Username string `json:"username"`
|
| }
|
|
|
|
|
| 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
|
| }
|
|
|
| user, token, err := h.authService.Login(req.Username, req.Password)
|
| if err != nil {
|
| c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid username or password"})
|
| return
|
| }
|
|
|
| c.JSON(http.StatusOK, LoginResponse{
|
| Token: token,
|
| User: User{
|
| ID: user.ID,
|
| Username: user.Username,
|
| },
|
| })
|
| }
|
|
|
|
|
| func (h *AuthHandler) Logout(c *gin.Context) {
|
| userID, ok := middleware.GetUserID(c)
|
| if !ok {
|
| c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
| return
|
| }
|
|
|
| if err := h.authService.Logout(userID); err != nil {
|
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to logout"})
|
| return
|
| }
|
|
|
| c.JSON(http.StatusOK, gin.H{"message": "logged out successfully"})
|
| }
|
|
|
|
|
| func (h *AuthHandler) Me(c *gin.Context) {
|
| userID, ok := middleware.GetUserID(c)
|
| if !ok {
|
| c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
| return
|
| }
|
|
|
| user, err := h.authService.GetUserByID(userID)
|
| if err != nil {
|
| c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
| return
|
| }
|
|
|
| c.JSON(http.StatusOK, User{
|
| ID: user.ID,
|
| Username: user.Username,
|
| })
|
| }
|
|
|
|
|
| func (h *AuthHandler) ChangePassword(c *gin.Context) {
|
| var req types.ChangePasswordRequest
|
| if err := c.ShouldBindJSON(&req); err != nil {
|
| c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误: " + err.Error()})
|
| return
|
| }
|
|
|
|
|
| userID, ok := middleware.GetUserID(c)
|
| if !ok {
|
| c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
| return
|
| }
|
|
|
|
|
| if err := h.authService.ChangePassword(userID, req.OldPassword, req.NewPassword); err != nil {
|
| if err == service.ErrInvalidCredentials {
|
| c.JSON(http.StatusBadRequest, gin.H{"error": "原密码错误"})
|
| return
|
| }
|
| c.JSON(http.StatusInternalServerError, gin.H{"error": "密码修改失败"})
|
| return
|
| }
|
|
|
| c.JSON(http.StatusOK, gin.H{"message": "密码修改成功"})
|
| } |