Hi,
I need to retrieve the username of the currently logged in user for my shopify app.
App is based on shopify remix and queryAPI.
Is there such a possibility?
In a Shopify app, you typically wonāt have direct access to the username of the currently logged-in user due to privacy and security reasons. However, you can access the customerās information if they are logged into their account on your store. Hereās how you can retrieve customer information including their username in a Shopify app:
-
Using Shopify APIs: You can use the Shopify API to retrieve customer information. When a customer is logged in and interacts with your app, you can make API requests to fetch details about the currently logged-in customer.
-
Customer Access Token: When a customer installs your app or logs in, your app can obtain a customer access token. With this token, you can make requests to the Shopify API on behalf of the customer to fetch their information, including their username if itās available.
Hereās an example of how you might retrieve customer information including their username using Shopifyās GraphQL API in a Shopify app built on Shopifyās app framework:
query {
customer(customerAccessToken: "CUSTOMER_ACCESS_TOKEN") {
id
firstName
lastName
email
displayName
...
}
}
Replace āāCUSTOMER_ACCESS_TOKENāā with the access token you obtained for the logged-in customer. This query will return details about the customer, including their first name, last name, email, and display name, which could be considered their username.
I forgot to mention that the application I am creating is of the embeded type. The solution you provided is directed to Storefront Api.
There is something similar for embeded apps?
Iām referring to the information of the user who is currently using the app.
Lets say you are working on shopify app it has an embedded shopify instance called ShopifyGlobal with variable name shopify it consist of a property called user. You can find further about this here.
https://shopify.dev/docs/api/app-bridge-library/apis/user
@Lucifer4
Thought Iād pitch in hereā¦
You can get the current user (of your app) email by adding to your shopify.server.js
useOnlineTokens: true,
And then you can update your remix loader to get info from session.
export const loader = async ({ request }) => {
const {session } = await authenticate.admin(request);
console.log(session.onlineAccessInfo.associated_user.id, "THE USER ID");
console.log(session.onlineAccessInfo.associated_user.email, "THE USER EMAIL")
return null;
};
Thanks @RMedia , this works but in my case, the associated_user only includes id. Do you have any idea how I can get more data about the user?
Hi, in my case I only needed the store ownerās name and email to create an account in my backend for the store that installed the app.
What I did was:
- First, I retrieved the session token from the app.
- Then, I sent the session token to my backend, where I exchanged it for an access token.
- Once I had the access token, I used the following GraphQL API query to retrieve the store ownerās information:
query shop {
shop {
name
shopOwnerName
email
}
}