The JavaScript Management SDK supports OAuth 2.0, enabling secure, token-based access to Contentstack APIs. It handles token acquisition, refresh, and storage automatically, simplifying the authentication flow.
You can use this integration across web applications, CLI tools, and other platforms to enable seamless authentication.
Note: OAuth support requires @contentstack/management version 1.20.0 or later and a registered OAuth app with Contentstack.
The SDK guides you through the full OAuth 2.0 flow in a streamlined way. Here’s how it works, step-by-step:
During initialization, configure OAuth with the required credentials.
import * as contentstack from '@contentstack/management'
const contentstackClient = contentstack.client()
const oauthHandler = contentstackClient.oauth({appId: 'your-app-id', clientId: 'your-client-id',redirectUri: 'Redirect-Uri'})
Parameters:
Parameter | Type | Description |
|---|---|---|
appId | Required | Your registered App ID |
clientId | Required | Your OAuth Client ID |
redirectUri | Required | The URL where the user is redirected after login and consent |
responseType | Optional | Set to code by default. You can customize it based on your OAuth settings. |
clientSecret | Optional | Required for standard OAuth flows (skip if using PKCE) |
scope | Optional | Permissions requested, such as read-only or full access, depending on your app’s requirements |
The authorize() method redirects the user to Contentstack’s OAuth server to login and authorize your app. To log in, use the code below:
oauthHandler.authorize();
After authorization, the server redirects the user back to your redirect_uri with an authorization code. Handle this redirect in your app using the handleRedirect() method.
// Assuming the redirect URL has query parameters like ?code=authorization_code
oauthHandler.handleRedirect(window.location.href);
The handleRedirect() method automatically processes the authorization code and retrieves access and refresh tokens.
The SDK securely stores tokens in memory. To access them manually, use the following code:
const accessToken = oauthHandler.getAccessToken();
const refreshToken = oauthHandler.getRefreshToken();
You can store the tokens in sessionStorage, localStorage, or cookies, depending on your use case.
Once the tokens are obtained, use the access token to make authenticated API requests. The SDK automatically appends the token to the Authorization header as a Bearer token for all outgoing requests.
try {
const user = await contentstackClient.getUser();
console.log(user);
} catch (error) {
console.error('Failed to fetch user:', error);
}If your access token expires, the SDK uses the refresh token to request a new one.
oauthHandler.refreshAccessToken()
.then(newAccessToken => {
console.log('New Access Token:', newAccessToken);
})
.catch(error => {
console.error('Failed to refresh access token:', error);
});
This ensures that your application continues to make authenticated requests without requiring the user to log in again.
The logout() method logs out the user and revokes authorization:
oauthHandler.logout();
This clears all your saved tokens and authorizations associated with the session.
After authentication, tokens are managed in memory. However, if needed, you can store them using the following methods:
Choose a storage strategy based on session duration and security:
sessionStorage.setItem('access_token', oauthHandler.getAccessToken());
localStorage.setItem('access_token', oauthHandler.getAccessToken());document.cookie = `access_token=${oauthHandler.getAccessToken()}; path=/; Secure; HttpOnly`;
Choose a storage strategy based on session duration and security:.
For sample implementation, refer to the code on GitHub.