mutationCustomerAccessTokenRenew.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package graphql
  2. import (
  3. "context"
  4. "github.com/gshopify/service-wrapper/auth"
  5. "github.com/gshopify/service-wrapper/scalar"
  6. "gshopper.com/gshopify/customer/graphql/generated"
  7. "gshopper.com/gshopify/customer/model"
  8. )
  9. func (r *mutationResolver) CustomerAccessTokenRenew(ctx context.Context, t string) (*generated.CustomerAccessTokenRenewPayload, error) {
  10. var (
  11. response = &generated.CustomerAccessTokenRenewPayload{}
  12. session = auth.SessionManager()
  13. claims *model.Claims
  14. err error
  15. )
  16. _, claims, err = r.decodeAccessToken(ctx, t)
  17. if err != nil {
  18. response.UserErrors = append(response.UserErrors, ErrTokenNotExists)
  19. return response, nil
  20. }
  21. refresh, err := session.Token(ctx, claims.SessionId)
  22. if err != nil {
  23. response.UserErrors = append(response.UserErrors, ErrToken(err.Error()))
  24. return response, nil
  25. }
  26. token, err := r.client.RefreshToken(ctx, refresh, r.conf.Cli.ClientId, r.conf.Cli.ClientSecret, r.conf.Cli.Realm)
  27. if err != nil {
  28. response.UserErrors = append(response.UserErrors, ErrTokenExpired)
  29. return response, nil
  30. }
  31. if err = r.saveSession(ctx, token); err != nil {
  32. response.UserErrors = append(response.UserErrors, ErrToken(err.Error()))
  33. return response, nil
  34. }
  35. response.CustomerAccessToken = &generated.CustomerAccessToken{
  36. AccessToken: token.AccessToken,
  37. ExpiresAt: scalar.NewDateTimeIn(token.RefreshExpiresIn),
  38. }
  39. return response, nil
  40. }