login_auth.go 771 B

1234567891011121314151617181920212223242526272829303132333435
  1. package middleware
  2. import (
  3. "youngee_b_api/consts"
  4. "youngee_b_api/model/redis_model"
  5. "youngee_b_api/service"
  6. "youngee_b_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. if auth, err := service.LoginAuth.AuthToken(c, token); err == nil {
  14. c.Set(consts.SessionAuthSchema, auth)
  15. c.Next()
  16. return
  17. } else {
  18. log.Infof("[LoginAuthMiddleware] auth fail,err:%+v", err)
  19. }
  20. }
  21. util.PackErrorResp(c, consts.ErrorNotLogin)
  22. c.Abort()
  23. }
  24. func GetSessionAuth(c *gin.Context) *redis_model.Auth {
  25. auth, contain := c.Get(consts.SessionAuthSchema)
  26. if contain {
  27. return auth.(*redis_model.Auth)
  28. }
  29. return new(redis_model.Auth)
  30. }