用户登出
This commit is contained in:
parent
6df3451829
commit
d65489d672
@ -17,6 +17,7 @@ type IAuthV1 interface {
|
|||||||
AuthRegister(ctx context.Context, req *request.RegisterReq) (res *request.RegisterRes, err error)
|
AuthRegister(ctx context.Context, req *request.RegisterReq) (res *request.RegisterRes, err error)
|
||||||
AuthLogin(ctx context.Context, req *request.LoginReq) (res *request.LoginRes, err error)
|
AuthLogin(ctx context.Context, req *request.LoginReq) (res *request.LoginRes, err error)
|
||||||
AuthCheck(ctx context.Context, req *request.CheckReq) (res *request.CheckRes, err error)
|
AuthCheck(ctx context.Context, req *request.CheckReq) (res *request.CheckRes, err error)
|
||||||
|
AuthLogout(ctx context.Context, req *request.LogoutReq) (res *request.LogoutRes, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ITokenV1 interface {
|
type ITokenV1 interface {
|
||||||
|
@ -13,7 +13,11 @@ type LoginReq struct {
|
|||||||
type CheckReq struct {
|
type CheckReq struct {
|
||||||
g.Meta `path:"/check" tags:"检查登录" method:"get" summary:"检查登录"`
|
g.Meta `path:"/check" tags:"检查登录" method:"get" summary:"检查登录"`
|
||||||
}
|
}
|
||||||
|
type LogoutReq struct {
|
||||||
|
g.Meta `path:"/logout" tags:"登出" method:"delete" summary:"登出账号"`
|
||||||
|
}
|
||||||
|
|
||||||
type RegisterRes struct{}
|
type RegisterRes struct{}
|
||||||
type LoginRes struct{}
|
type LoginRes struct{}
|
||||||
type CheckRes struct{}
|
type CheckRes struct{}
|
||||||
|
type LogoutRes struct{}
|
||||||
|
@ -11,6 +11,10 @@ import (
|
|||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func userService() UserService.UserService {
|
||||||
|
return UserService.NewUserService()
|
||||||
|
}
|
||||||
|
|
||||||
// AuthRegister
|
// AuthRegister
|
||||||
//
|
//
|
||||||
// 用户注册
|
// 用户注册
|
||||||
@ -23,8 +27,7 @@ func (*ControllerV1) AuthRegister(ctx context.Context, _ *request.RegisterReq) (
|
|||||||
errStruct := g.Validator().Data(userRegister).Run(ctx)
|
errStruct := g.Validator().Data(userRegister).Run(ctx)
|
||||||
if errStruct == nil {
|
if errStruct == nil {
|
||||||
// 进行用户注册
|
// 进行用户注册
|
||||||
userService := UserService.NewUserService()
|
userService().UserRegister(req, &userRegister)
|
||||||
userService.UserRegister(req, &userRegister)
|
|
||||||
} else {
|
} else {
|
||||||
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
|
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
|
||||||
}
|
}
|
||||||
@ -43,8 +46,7 @@ func (*ControllerV1) AuthLogin(ctx context.Context, _ *request.LoginReq) (res *r
|
|||||||
errStruct := g.Validator().Data(userLogin).Run(ctx)
|
errStruct := g.Validator().Data(userLogin).Run(ctx)
|
||||||
if errStruct == nil {
|
if errStruct == nil {
|
||||||
// 进行用户注册
|
// 进行用户注册
|
||||||
userService := UserService.NewUserService()
|
userService().UserLogin(req, &userLogin)
|
||||||
userService.UserLogin(req, &userLogin)
|
|
||||||
} else {
|
} else {
|
||||||
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
|
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
|
||||||
}
|
}
|
||||||
@ -60,7 +62,16 @@ func (*ControllerV1) AuthLogin(ctx context.Context, _ *request.LoginReq) (res *r
|
|||||||
func (*ControllerV1) AuthCheck(ctx context.Context, _ *request.CheckReq) (res *request.CheckRes, err error) {
|
func (*ControllerV1) AuthCheck(ctx context.Context, _ *request.CheckReq) (res *request.CheckRes, err error) {
|
||||||
req := ghttp.RequestFromCtx(ctx)
|
req := ghttp.RequestFromCtx(ctx)
|
||||||
// 获取数据库中用户信息
|
// 获取数据库中用户信息
|
||||||
userService := UserService.NewUserService()
|
userService().CheckLogin(req)
|
||||||
userService.CheckLogin(req)
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthLogout
|
||||||
|
//
|
||||||
|
// 用户登出
|
||||||
|
func (*ControllerV1) AuthLogout(ctx context.Context, _ *request.LogoutReq) (res *request.LogoutRes, err error) {
|
||||||
|
req := ghttp.RequestFromCtx(ctx)
|
||||||
|
// 获取数据库中用户信息
|
||||||
|
userService().UserLogout(req)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
@ -77,3 +77,21 @@ func (_ DefaultTokenImpl) LoginToken(req *ghttp.Request, userDO do.UserDO) *do.T
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteToken
|
||||||
|
//
|
||||||
|
// 删除Token业务
|
||||||
|
func (_ DefaultTokenImpl) DeleteToken(req *ghttp.Request) bool {
|
||||||
|
// 获取 Cookie 中的 token
|
||||||
|
cookieToken := req.Cookie.Get("token")
|
||||||
|
if cookieToken != nil {
|
||||||
|
// 数据库查找 token
|
||||||
|
token := tokenDAO.GetToken(cookieToken.String())
|
||||||
|
// 检查数据库中是否存在该 token
|
||||||
|
if token != nil {
|
||||||
|
// 删除数据库
|
||||||
|
return tokenDAO.DeleteToken(token.Token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -98,3 +98,14 @@ func (*DefaultUserImpl) CheckLogin(req *ghttp.Request) {
|
|||||||
ResultUtil.ErrorNoData(req, ErrorCode.UserNotExist)
|
ResultUtil.ErrorNoData(req, ErrorCode.UserNotExist)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserLogout
|
||||||
|
//
|
||||||
|
// 用户登出
|
||||||
|
func (*DefaultUserImpl) UserLogout(req *ghttp.Request) {
|
||||||
|
if tokenService().DeleteToken(req) {
|
||||||
|
ResultUtil.Success(req, "用户已退出登录", nil)
|
||||||
|
} else {
|
||||||
|
ResultUtil.ErrorNoData(req, ErrorCode.TokenNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,7 +4,9 @@ import (
|
|||||||
"PersonalMain/internal/service/TokenService"
|
"PersonalMain/internal/service/TokenService"
|
||||||
"PersonalMain/utility/ErrorCode"
|
"PersonalMain/utility/ErrorCode"
|
||||||
"PersonalMain/utility/ResultUtil"
|
"PersonalMain/utility/ResultUtil"
|
||||||
|
"context"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/text/gregex"
|
"github.com/gogf/gf/v2/text/gregex"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -26,8 +28,10 @@ type DefaultHandlerResponse struct {
|
|||||||
// 检查时间戳误差是否在
|
// 检查时间戳误差是否在
|
||||||
func TimestampMiddleware(r *ghttp.Request) {
|
func TimestampMiddleware(r *ghttp.Request) {
|
||||||
// 检查时间戳误差是否在
|
// 检查时间戳误差是否在
|
||||||
timestamp, err := strconv.ParseInt(r.GetHeader("timestamp"), 10, 64)
|
timestampString := r.GetHeader("timestamp")
|
||||||
if gregex.IsMatch(`^[0-9]{13,14}$`, []byte(strconv.FormatInt(timestamp, 10))) {
|
if len(timestampString) >= 13 && len(timestampString) <= 14 {
|
||||||
|
timestamp, err := strconv.ParseInt(timestampString, 10, 64)
|
||||||
|
if gregex.IsMatch(`^[0-9]+$`, []byte(strconv.FormatInt(timestamp, 10))) {
|
||||||
if timestamp+int64(2000) > time.Now().UnixMilli() && timestamp-int64(2000) < time.Now().UnixMilli() {
|
if timestamp+int64(2000) > time.Now().UnixMilli() && timestamp-int64(2000) < time.Now().UnixMilli() {
|
||||||
r.Middleware.Next()
|
r.Middleware.Next()
|
||||||
} else {
|
} else {
|
||||||
@ -40,6 +44,9 @@ func TimestampMiddleware(r *ghttp.Request) {
|
|||||||
ResultUtil.ErrorNoData(r, ErrorCode.TimestampVerifyFailed)
|
ResultUtil.ErrorNoData(r, ErrorCode.TimestampVerifyFailed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ResultUtil.ErrorNoData(r, ErrorCode.TimestampVerifyFailed)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyTokenMiddleware
|
// VerifyTokenMiddleware
|
||||||
@ -101,6 +108,7 @@ func JsonResponseMiddleware(r *ghttp.Request) {
|
|||||||
msg = ErrorCode.ServerUnknownError.Message()
|
msg = ErrorCode.ServerUnknownError.Message()
|
||||||
output = ErrorCode.ServerUnknownError.Output()
|
output = ErrorCode.ServerUnknownError.Output()
|
||||||
}
|
}
|
||||||
|
g.Log().Cat("Unknown").Info(context.Background(), output, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,4 +18,5 @@ type TokenService interface {
|
|||||||
GetToken(*ghttp.Request) *do.TokenDO
|
GetToken(*ghttp.Request) *do.TokenDO
|
||||||
VerifyToken(*do.TokenDO) bool
|
VerifyToken(*do.TokenDO) bool
|
||||||
LoginToken(*ghttp.Request, do.UserDO) *do.TokenDO
|
LoginToken(*ghttp.Request, do.UserDO) *do.TokenDO
|
||||||
|
DeleteToken(*ghttp.Request) bool
|
||||||
}
|
}
|
||||||
|
@ -14,4 +14,5 @@ type UserService interface {
|
|||||||
UserRegister(*ghttp.Request, *entity.UserRegisterVO)
|
UserRegister(*ghttp.Request, *entity.UserRegisterVO)
|
||||||
UserLogin(*ghttp.Request, *entity.UserLoginVO)
|
UserLogin(*ghttp.Request, *entity.UserLoginVO)
|
||||||
CheckLogin(*ghttp.Request)
|
CheckLogin(*ghttp.Request)
|
||||||
|
UserLogout(*ghttp.Request)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user