feat: login and logout

This commit is contained in:
Leon
2025-12-05 14:03:57 +08:00
parent b2cf39e731
commit 35938aaa13
5 changed files with 104 additions and 2 deletions

View File

@@ -53,6 +53,19 @@ type ChangePasswordRequest struct {
type ChangePasswordResponse struct {
}
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
type LoginResponse struct {
Token string `json:"token"`
}
type LogoutRequest struct {
ID uint `json:"id"`
}
type LogoutResponse struct {
}
func NewUserHandler(userService *account.UserService) *UserHandler {
return &UserHandler{userService: userService}
}
@@ -91,7 +104,7 @@ func (h *UserHandler) ChangePassword(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.userService.ChangePassword(req.ID, req.NewPassword); err != nil {
if err := h.userService.ChangePassword(req.Username, req.OldPassword, req.NewPassword); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
@@ -125,3 +138,30 @@ func (h *UserHandler) FindByUsername(c *gin.Context) {
c.JSON(200, user)
}
}
func (h *UserHandler) Login(c *gin.Context) {
var req LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if token, err := h.userService.Login(req.Username, req.Password); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
} else {
c.JSON(200, LoginResponse{Token: token})
}
}
func (h *UserHandler) Logout(c *gin.Context) {
var req LogoutRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.userService.Logout(req.ID); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, LogoutResponse{})
}

View File

@@ -2,6 +2,7 @@ package http
import (
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -21,6 +22,16 @@ func SetRouter(db *gorm.DB) *gin.Engine {
userGroup.POST("/findByID", userHandler.FindByID)
userGroup.POST("/findByUsername", userHandler.FindByUsername)
}
authGroup := r.Group("/auth")
{
authGroup.POST("/login", userHandler.Login)
}
protectedAuthGroup := authGroup.Group("")
protectedAuthGroup.Use(middleware.JWTAuth())
{
protectedAuthGroup.POST("/logout", userHandler.Logout)
}
return r
}