setToken
Sets the Growave JWT token manually.
This method saves the token to localStorage. To avoid unnecessary overwrites, use getShopifyCustomerId() to check if the customer is already authenticated.
setToken() does not transfer the previous guest session by itself. The session carries over only if your backend passed the guest's sessionToken to getStorefrontToken when it obtained the token.
Use this method only when authentication occurs after SDK.init() and the token could not be provided immediately.
If the token is known before SDK initialization, pass it via the growaveToken option in SDK.init().
To obtain a Growave JWT token, use the Private API endpoint: getStorefrontToken
sdk.authentication.methods.setToken()Usage example
Ensure the SDK is ready and the AuthenticationSdk module is initialized, then call setToken.
// Obtain token from your server (calls Growave Private API)
const token = await fetchGrowaveTokenFromServer();
function onSdkReady() {
window.gw.sdk.runIfModuleReady(
window.gw.sdk.common.constants.modules.AUTHENTICATION,
() => {
// Check if already authenticated as this customer
const currentCustomerId = window.gw.sdk.authentication.methods.getShopifyCustomerId();
if (currentCustomerId !== newCustomerId) {
window.gw.sdk.authentication.methods.setToken(token);
console.log("Token set successfully");
}
}
);
}
if (window.gw && window.gw.sdk) {
onSdkReady();
} else {
document.addEventListener("gwSdkReady", onSdkReady);
}