2 Star 2 Fork 0

skyzhou / fangfang-api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
check_login.go 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
skyzhou 提交于 2022-07-23 08:36 . 提交至gitee
package interceptor
import (
"encoding/json"
"net/http"
"gitee.com/sky-zhou/fangfang-api/config"
"gitee.com/sky-zhou/fangfang-api/internal/code"
"gitee.com/sky-zhou/fangfang-api/internal/pkg/core"
"gitee.com/sky-zhou/fangfang-api/internal/proposal"
"gitee.com/sky-zhou/fangfang-api/internal/repo/redis"
"gitee.com/sky-zhou/fangfang-api/pkg/errors"
)
func (i *interceptor) CheckLogin(ctx core.Context) (sessionUserInfo proposal.SessionUserInfo, err core.BusinessError) {
token := ctx.GetHeader(config.HeaderLoginToken)
if token == "" {
err = core.Error(
http.StatusUnauthorized,
code.AuthorizationError,
code.Text(code.AuthorizationError)).WithError(errors.New("Header 中缺少 Token 参数"))
return
}
if !i.cache.Exists(config.RedisKeyPrefixLoginUser + token) {
err = core.Error(
http.StatusUnauthorized,
code.AuthorizationError,
code.Text(code.AuthorizationError)).WithError(errors.New("请先登录"))
return
}
cacheData, cacheErr := i.cache.Get(config.RedisKeyPrefixLoginUser+token, redis.WithTrace(ctx.Trace()))
if cacheErr != nil {
err = core.Error(
http.StatusUnauthorized,
code.AuthorizationError,
code.Text(code.AuthorizationError)).WithError(cacheErr)
return
}
jsonErr := json.Unmarshal([]byte(cacheData), &sessionUserInfo)
if jsonErr != nil {
core.Error(
http.StatusUnauthorized,
code.AuthorizationError,
code.Text(code.AuthorizationError)).WithError(jsonErr)
return
}
return
}
Go
1
https://gitee.com/sky-zhou/fangfang-api.git
git@gitee.com:sky-zhou/fangfang-api.git
sky-zhou
fangfang-api
fangfang-api
e5d1c0ad8e52

搜索帮助