33 lines
725 B
Go
33 lines
725 B
Go
package filter
|
|
|
|
import (
|
|
"context"
|
|
"mylomen_server/common/constant"
|
|
"mylomen_server/infrastructure/repository"
|
|
"mylomen_server/service"
|
|
"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 {
|
|
fbc := context.WithValue(context.Background(), constant.DBKey, repository.Db)
|
|
loginResult := service.Login.GetLoginResult(fbc, &c)
|
|
if loginResult == nil {
|
|
return c.JSON(http.StatusOK, unauthorizedResp)
|
|
}
|
|
|
|
//存入数据
|
|
c.Set(constant.UID, loginResult.Uid)
|
|
|
|
return next(c)
|
|
}
|
|
}
|