面包店
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

47 rindas
949 B

  1. package svc
  2. import (
  3. "applet/app/bigData/lib/auth"
  4. "applet/app/bigData/md"
  5. "errors"
  6. "github.com/gin-gonic/gin"
  7. "strings"
  8. )
  9. func GetUser(c *gin.Context) *md.User {
  10. user, _ := c.Get("user")
  11. if user == nil {
  12. return &md.User{
  13. Account: "",
  14. Password: "",
  15. LoginKind: "",
  16. }
  17. }
  18. return user.(*md.User)
  19. }
  20. func CheckUser(c *gin.Context) (*md.User, error) {
  21. token := c.GetHeader("Authorization")
  22. if token == "" {
  23. return nil, errors.New("token not exist")
  24. }
  25. // 按空格分割
  26. parts := strings.SplitN(token, " ", 2)
  27. if !(len(parts) == 2 && parts[0] == "Bearer") {
  28. return nil, errors.New("token format error")
  29. }
  30. // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  31. mc, err := auth.ParseToken(parts[1])
  32. if err != nil {
  33. return nil, err
  34. }
  35. // 获取user
  36. user := &md.User{
  37. Account: mc.Account,
  38. Password: mc.Password,
  39. LoginKind: mc.LoginKind,
  40. }
  41. return user, nil
  42. }