33 lines
725 B
Go
Raw Normal View History

2023-06-04 22:54:54 +08:00
package filter
import (
2024-02-21 12:04:40 +08:00
"context"
2024-09-29 14:08:55 +08:00
"mylomen_server/common/constant"
"mylomen_server/infrastructure/repository"
"mylomen_server/service"
2023-06-04 22:54:54 +08:00
"net/http"
"github.com/labstack/echo/v4"
)
var unauthorizedResp = map[string]interface{}{
"code": http.StatusUnauthorized,
"msg": "Login failed, please try again.",
}
// UserAuth 新版本
func UserAuth(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
2024-02-21 12:04:40 +08:00
fbc := context.WithValue(context.Background(), constant.DBKey, repository.Db)
2023-06-04 22:54:54 +08:00
loginResult := service.Login.GetLoginResult(fbc, &c)
if loginResult == nil {
return c.JSON(http.StatusOK, unauthorizedResp)
}
//存入数据
c.Set(constant.UID, loginResult.Uid)
return next(c)
}
}