Managing Access Tokens
This is a general overview of access token management, including information about best practices and error handling. For a more detailed breakdown of the steps for retrieving and refreshing access tokens, see the Authorization Overview.
Authorization Steps
1. Initiate Authorization
For every business account you wish to access:
- Direct the end-user to your configured Lightspeed authorization URL.
- Users will be prompted to login to Lightspeed and grant permissions based on the
scopeparameter provided in the authorization URL. - Once permissions are granted, the redirect URL is called with a temporary authorization
code.
The state parameter, typically a randomly generated string used for security purposes, can also be used to match which business account corresponds to each authorization request. By associating a randomly generated state value with each account, you can determine which account's authorization is being processed upon redirection.
2. Retrieve the Access Token
After receiving the temporary authorization code:
- Exchange this code for an
access_tokenandrefresh_tokenpair by sending a request to the/tokenendpoint. - The response will contain both the
access_tokenand therefresh_token. It will also contain thescopeandexpires_indetails.
Securely store this data. Relate each access token and corresponding refresh token with the respective business location ID, or another unique identifier for tracking purposes.
3. Token Management and Refresh
- When the token is nearing expiration, use the
refresh_tokento request new tokens.
Replace outdated tokens with the newly acquired ones for that specific business.
Best Practices for Managing Multiple Tokens
- Secure Storage: Access tokens and refresh tokens should remain within your server environment to ensure maximum security. Never store access tokens or refresh tokens in web applications on the frontend, and avoid storing them within your app, whenever possible.
- Automated Refresh Mechanism: Implement automation for token expiration checks and proactive refreshing. Ideally, access tokens should be refreshed within a 30 second window of their expiration time.
- Log and Monitor: Keep logs to monitor token generation, refreshing, and failures, to assist in identifying integration issues.
Error Handling
If an access token becomes invalid or expired, a 4xx error will be returned. In this case, the authorization flow should be re-initiated. This means prompting the user to re-authenticate so the application can secure a new access token.
For more information on OAuth2, see the framework documentation.