1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package auth
- import (
- "authService/response"
- "authService/service/user"
- "authService/util"
- "authService/util/constants"
- "authService/validators"
- "github.com/gin-gonic/gin"
- )
- // 登录
- func Login(c *gin.Context) {
- type Validator struct {
- Account string `json:"account" form:"account" binding:"required,alphanum,min=4,max=20"`
- Password string `json:"password" form:"password" binding:"required,min=6,max=20"`
- }
- var validator Validator
- err := c.ShouldBind(&validator)
- if err != nil {
- response.FailValidator(c, err)
- return
- }
- token, expireIn, errCode := user.Login(validator.Account, validator.Password)
- if errCode != nil {
- response.Fail(c, errCode)
- return
- }
- response.Success(c, map[string]any{
- "token": token,
- "expireIn": expireIn,
- })
- }
- // 退出登录
- func Logout(c *gin.Context) {
- cacheUser, _ := util.GetFromGinContext[*validators.AuthUser](c, constants.UserCacheKey)
- user.Logout(cacheUser.ID)
- response.Success(c, map[string]any{})
- }
|