Happening now! Shopify Community AMA: Building a Marketing Budget with 2H Media | Ask your marketing budget questions now!

Query for Customer returning nil with a newly created access token

Query for Customer returning nil with a newly created access token

jig_r
Shopify Partner
33 0 8

Hello, 

Like the title says there is something weird going on that I am not understanding.

Using a newly created user storefront access token (via customer email and password), when I try to query the customer object it comes back as null.

This is for a newly created account.

Anyone can help to try rubber duck and figure out if there something I am missing?

Replies 6 (6)

David_Weru
Shopify Partner
178 16 43

Hello,

 

To my understanding, you're dealing with 2 very different things that don't hold hands per se.

StorefrontAccessToken -This is set to give api access to your store.  Checking orders, plugins, paid themes, that sort of thing.

For you to have "back stage access" to a store (or more appropriately for your plugin to have access to someone's store data) you have to give them access.

This access token is a part of that security handshake.

->more on that here: https://shopify.dev/docs/admin-api/rest/reference/access/storefrontaccesstoken

 

 

Customer -This object gets created once the customer has been logged in.  -Yes, the customer must exist in the store to log in, but for this object to exist in liquid, you have to have the actual customer logged in.

->more on that here: https://shopify.dev/docs/themes/liquid/reference/objects/customer

 

Short answer: If you view the site without being logged in, the customer object won't be created.  Calling it will not return a customer object.  Hence nil.

 

jig_r
Shopify Partner
33 0 8

Ahh okay I think you might have miss understood what I was trying to say or maybe I haven’t wrote this clearly.

 
When I talk about storefront access token I’m not talking about the access to the API.
 
I’m talking specifically about the user access token. 
 
So flow is:
 
I create a new user account using email password.
 
Using that email and password I make a mutation to customerAccessTokenCreate.
 
This returns an accessToken. Let’s call this user access token.
 
Using the user access token when I try to call query customer(customerAccessToken: )
 
It returns null.
 
So what I’m wondering is whether there’s something funny going on on my side that I haven’t considered: 
 
Such as something to do with the user account not being activated, or the API version not being the same or something along those lines.
 
Now to be clear I’m not looking for a specific solution to a specific problem, I’m wondering what are the scenarios that this might happen.

Best, 
 
Jigar 

Sent from my iPhone
David_Weru
Shopify Partner
178 16 43

Oookay.

 

If you post the code, it might've helped.

https://shopify.dev/docs/storefront-api/reference/customers/customeraccesstokencreate#return-fields-...

This is what I'm now guessing you're referring to.

 

I'm I close?

diegojacomel
Shopify Partner
2 0 0

I have the same problem here and I did all process on Shopify GraphiQL App

adrianiskandar
Visitor
1 0 0

I'm having the same problem. I'm creating a custom storefront using the shopify graphql. So on my login page when I log in the user using the customerAccessTokenCreate everything works fine:

 

```

const query = gql(/* GraphQL */ `
mutation customerAccessTokenCreate($input: CustomerAccessTokenCreateInput!) {
customerAccessTokenCreate(input: $input) {
customerUserErrors {
code
field
message
}
customerAccessToken {
accessToken
expiresAt
}
}
}
`);

const variables = {
input: { email, password },
};
```

In the result I don't get errors and I receive an access token. But when I try to use that access token for other queries, like getting the customer info:

 

query getCustomer($customerAccessToken: String!) {
customer(customerAccessToken: $customerAccessToken) {
acceptsMarketing
addresses(last: 5) {
edges {
node {
address1
address2
city
company
country
countryCodeV2
firstName
formatted
formattedArea
id
lastName
latitude
longitude
name
phone
province
provinceCode
zip
}
}
}
createdAt
displayName
email
firstName
id
lastName
numberOfOrders
phone
updatedAt
}
}

query variables:

{
"customerAccessToken": "theAccessTokenIJustCreated"
}

The result is always null, which is wrong, so can you please tell me what is happening here?
{
"data": {
"customer": null
}
}

David_Weru
Shopify Partner
178 16 43

Hello, @adrianiskandar and @diegojacomel ,

 

There's a lot to unpack here as far as how all this should work.

 

My suggestion which should cover all bases is:

1: find working example code and modify it.

 

2: That access token belongs to an account.  Check which permissions it has on your store.

 

2.1:  whatever the administrator of a store cannot see, the highest permission settings cannot see either.

 

2.2:  a lot of your store data is more open than you think, such as variants and prices.  Google "shopify store json"

 

3: shopify houses the userdata.  Other than basic stuff, access is/should be out of the store owner's hands unless it is tied to an order.

 

4: look up the api you are using weather you're using php or something else to ping for the data.  If the data is protected (used in system but not accessible outward)  you cannot query it.  You can try, but null data is what you will be given as a graceful crash.

 

This would be something I do for a fee, but I'm tied atm and can't give this the priority it deserves.  However, if you get further and would like assistance, just reply to this thread.

 

-David_Weru