login_auth.go 794 B

123456789101112131415161718192021222324252627282930313233343536
  1. package middleware
  2. import (
  3. "youngee_m_api/consts"
  4. "youngee_m_api/model/redis_model"
  5. "youngee_m_api/service"
  6. "youngee_m_api/util"
  7. log "github.com/sirupsen/logrus"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func LoginAuthMiddleware(c *gin.Context) {
  11. token := c.Request.Header.Get("Authorization")
  12. if token != "" {
  13. //fmt.Println(token)
  14. if auth, err := service.LoginAuth.AuthToken(c, token); err == nil {
  15. c.Set(consts.SessionAuthSchema, auth)
  16. c.Next()
  17. return
  18. } else {
  19. log.Infof("[LoginAuthMiddleware] auth fail,err:%+v", err)
  20. }
  21. }
  22. util.PackErrorResp(c, consts.ErrorNotLogin)
  23. c.Abort()
  24. }
  25. func GetSessionAuth(c *gin.Context) *redis_model.Auth {
  26. auth, contain := c.Get(consts.SessionAuthSchema)
  27. if contain {
  28. return auth.(*redis_model.Auth)
  29. }
  30. return new(redis_model.Auth)
  31. }