You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CL Authentication API is rate-limited to 50 req/s, background jobs and static generations need to cache tokens to avoid errors
Refreshing tokens must be done manually
Ideas
Cache tokens automatically based on their scope until they expire
Requests automatically use the correct token for the current scope
Tokens are automatically refreshed if they expire
Can save tokens to cookies and load them from cookies
Proposal
Configuration
import{init}from'@atoms-studio/commercelayer-sdk'init({host: '<subdomain>.commercelayer.io',// Global client_id used for all Sales Channel requestsclientId: '<client_id>',// Attempt at refreshing expired tokens automaticallyrefreshTokens: true,refreshTokensAttempts: 5,// Saves cookies automatically.// Falsy values will disable the functionalitycookies: {customer_token: 'cl_customer_token',customer_refresh_token: 'cl_customer_refresh_token',}})
Usage
import{Auth}from'@atoms-studio/commercelayer-sdk'// Set current market(s).// Tokens will always use current market as scopeAuth.setMarket([1234,1237])// Request a guest token using the current market as scope.// Cache the token until it expires so future calls in the same // market will return the cached value.// We probably don't need to use the result of the promise for anything.Auth.loginAsGuest().then(({token: string,expires: Date})=>{})// Request a customer token using the credentials provided// and the current market as scope.// Save tokens in cookies if needed and configured.// Automatically chains a call to the profile endpoint to return// the customer information.Auth.loginAsCustomer({username: '<email>',password: '<password>',rememberMe: true,}).then(({token: string,expires: Date,customer: Customer,refreshToken: string})=>{})// Attempts to load the customer from the cookieawaitAuth.loadCustomer()// Returns the current customer if presentAuth.getCustomer()// Checks if a customer is currently logged inAuth.hasCustomer()// Logs out the current customerAuth.logout()// Handle refresh tokens failures.// This callback is invoked after the configured number of attempts.// In this case we redirect to the login page if a customer cannot refresh its token anymore.Auth.onRefreshError((err: Error,customer: Customer=null)=>{if(customer){router.push('/login')}else{error({statusCode: 500})}})// Request an integration tokenAuth.loginAsIntegration({clientId: '<client_id>',clientSecret: '<client_secret>',}).then(({token: string,expires: Date})=>{}
Issues
How can Integrations tokens work without polluting Sales Channels state?
Should we automatically call Auth.loginAsGuest() when using Auth.setMarket([1234, 1237])? What do we do when a customer is logged in?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Current pain points:
Ideas
Proposal
Configuration
Usage
Issues
Auth.loginAsGuest()
when usingAuth.setMarket([1234, 1237])
? What do we do when a customer is logged in?Beta Was this translation helpful? Give feedback.
All reactions