auth.initOauth
Initializes an OAuth client for a multi account.
Parameters
optionsInitMultiAccountOauthOptionsOptions for initializing the oauth client
providerstringThe provider to initialize the oauth client for
typestringThe type of oauth client to initialize
The only valid value is "multi_account".
Returns
A client for authorizing users with OAuth.
getAccountsfunctionReturns
The result of requesting a list of accounts. This is a Promise that resolves with the following object:
accountsOauthAccount[]A client for managing a single OAuth account.
idstringThe ID of the account.
displayNamestringThe display name of the account.
expiredbooleanWhether the access token for the account has expired and there is no associated refresh token.
getAccessTokenfunctionGets the access token and scopes for the current OAuth account.
- When a token expires, it's automatically refreshed by Canva.
- The token is cached by Canva, so the app's frontend shouldn't store the token.
Parameters
requestAccessTokenRequestOptions for requesting an access token for the current OAuth account.
forceRefreshbooleanIf true, the access token will be refreshed, even if it hasn't expired.
By default, access tokens are automatically refreshed after expiry.
scopeSet<string>The scopes associated with the access token.
Returns
The access token and scopes for the current OAuth account, or null if the account isn't authorized. This is a Promise that resolves with either undefined or the following object:
tokenstringThe access token for the current user.
scopeSet<string>The scopes associated with the current user's access token.
Examples
Get the access token of the current OAuth account
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth({ type: 'multi_account' });// Get the accountconst account = await oauth.getAccount({ accountId: '123' });// Get an access token for the accountconst token = await oauth.getAccessToken();
Check if the account is authorized
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth({ type: 'multi_account' });// Get an access token for the accountconst account = await oauth.getAccount({ accountId: '123' });const token = await account.getAccessToken();if (token) {// The account is authorized} else {// The account is not authorized}
Forcefully refresh an access token
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth({ type: 'multi_account' });// Get the accountconst account = await oauth.getAccount({ accountId: '123' });// Forcefully refresh an access tokenconst token = await account.getAccessToken({ forceRefresh: true });
principalstringThe principal of the account (e.g., email, username).
avatarUrlstringThe avatar URL of the account.
getAccountfunctionParameters
requestGetAccountRequestOptions for requesting a single OAuth account.
accountIdstringReturns
The result of requesting a single account. This is a Promise that resolves with the following object:
accountOauthAccountA client for managing a single OAuth account.
idstringThe ID of the account.
displayNamestringThe display name of the account.
expiredbooleanWhether the access token for the account has expired and there is no associated refresh token.
getAccessTokenfunctionGets the access token and scopes for the current OAuth account.
- When a token expires, it's automatically refreshed by Canva.
- The token is cached by Canva, so the app's frontend shouldn't store the token.
Parameters
requestAccessTokenRequestOptions for requesting an access token for the current OAuth account.
forceRefreshbooleanIf true, the access token will be refreshed, even if it hasn't expired.
By default, access tokens are automatically refreshed after expiry.
scopeSet<string>The scopes associated with the access token.
Returns
The access token and scopes for the current OAuth account, or null if the account isn't authorized. This is a Promise that resolves with either undefined or the following object:
tokenstringThe access token for the current user.
scopeSet<string>The scopes associated with the current user's access token.
Examples
Get the access token of the current OAuth account
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth({ type: 'multi_account' });// Get the accountconst account = await oauth.getAccount({ accountId: '123' });// Get an access token for the accountconst token = await oauth.getAccessToken();
Check if the account is authorized
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth({ type: 'multi_account' });// Get an access token for the accountconst account = await oauth.getAccount({ accountId: '123' });const token = await account.getAccessToken();if (token) {// The account is authorized} else {// The account is not authorized}
Forcefully refresh an access token
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth({ type: 'multi_account' });// Get the accountconst account = await oauth.getAccount({ accountId: '123' });// Forcefully refresh an access tokenconst token = await account.getAccessToken({ forceRefresh: true });
principalstringThe principal of the account (e.g., email, username).
avatarUrlstringThe avatar URL of the account.