|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package svc
-
- import (
- "applet/app/bigData/lib/auth"
- "applet/app/bigData/md"
- "errors"
- "github.com/gin-gonic/gin"
- "strings"
- )
-
- func GetUser(c *gin.Context) *md.User {
- user, _ := c.Get("user")
- if user == nil {
- return &md.User{
- Account: "",
- Password: "",
- LoginKind: "",
- }
- }
- return user.(*md.User)
- }
-
- func CheckUser(c *gin.Context) (*md.User, error) {
- token := c.GetHeader("Authorization")
- if token == "" {
- return nil, errors.New("token not exist")
- }
- // 按空格分割
- parts := strings.SplitN(token, " ", 2)
- if !(len(parts) == 2 && parts[0] == "Bearer") {
- return nil, errors.New("token format error")
- }
- // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
- mc, err := auth.ParseToken(parts[1])
- if err != nil {
- return nil, err
- }
-
- // 获取user
- user := &md.User{
- Account: mc.Account,
- Password: mc.Password,
- LoginKind: mc.LoginKind,
- }
- return user, nil
- }
|