mutationCustomerAccessTokenCreate.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package graphql
  2. import (
  3. "context"
  4. "github.com/Nerzal/gocloak/v12"
  5. "github.com/gshopify/service-wrapper/scalar"
  6. "gshopper.com/gshopify/customer/graphql/generated"
  7. )
  8. func (r *mutationResolver) CustomerAccessTokenCreate(
  9. ctx context.Context,
  10. input generated.CustomerAccessTokenCreateInput) (*generated.CustomerAccessTokenCreatePayload, error) {
  11. var (
  12. token *gocloak.JWT
  13. err error
  14. response = &generated.CustomerAccessTokenCreatePayload{}
  15. )
  16. if token, err = r.client.Login(
  17. ctx, r.conf.Cli.ClientId, r.conf.Cli.ClientSecret, r.conf.Cli.Realm, input.Email, input.Password); err != nil {
  18. response.CustomerUserErrors = append(response.CustomerUserErrors,
  19. CustomerError(generated.CustomerErrorCodeUnidentifiedCustomer, err))
  20. return response, nil
  21. }
  22. if err = r.saveSession(ctx, token); err != nil {
  23. response.CustomerUserErrors = append(response.CustomerUserErrors,
  24. CustomerError(generated.CustomerErrorCodeBlank, err))
  25. return response, nil
  26. }
  27. response.CustomerAccessToken = &generated.CustomerAccessToken{
  28. AccessToken: token.AccessToken,
  29. ExpiresAt: scalar.NewDateTimeIn(token.RefreshExpiresIn),
  30. }
  31. return response, nil
  32. }