authcheck.go 477 B

123456789101112131415161718192021
  1. package hook
  2. import (
  3. "context"
  4. "errors"
  5. "wechat-api/ent"
  6. tokenModel "wechat-api/ent/token"
  7. )
  8. // CheckDesktopAuth 检查token是否合法同时返回token实例
  9. func CheckDesktopAuth(tokenStr string, client *ent.Client) (*ent.Token, error) {
  10. if tokenStr == "" {
  11. return nil, errors.New("auth cannot be null")
  12. }
  13. token, err := client.Token.Query().Where(tokenModel.Token(tokenStr)).Only(context.Background())
  14. if err != nil {
  15. return nil, err
  16. }
  17. return token, nil
  18. }