Creates a client for authenticating users with OAuth.
To learn more, see https://www.canva.dev/docs/apps/authenticating-users/oauth.
Usage
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth();// Check if the user has a tokenconst token = await oauth.getAccessToken();// If an access token is available, the user is authorizedif (token) {// Use the token to implement behavior that requires authorizationconsole.log(token);} else {// Otherwise, start an authorization flowconst response = await oauth.requestAuthorization();if (response.status === "complete") {// The user successfully authorized// The app can now get the user's access token} else {// The user did not successfully authorize}}
Returns
A client for authorizing users with OAuth.
getAccessToken
function
Gets the access token and scopes for the current user.
- 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
request
AccessTokenRequest
Options for requesting an access token for the current user.
Properties of request
forceRefresh
boolean
If true
, the access token will be refreshed, even if it hasn't expired.
By default, access tokens are automatically refreshed after expiry.
scope
Set<string>
The scopes associated with the access token.
Returns
The access token and scopes for the current user, or null
if the user isn't authorized. This is a Promise
that resolves with either undefined
or the following object:
token
string
The access token for the current user.
scope
Set<string>
The scopes associated with the current user's access token.
Example
import { auth } from "@canva/user";// Initialize an OAuth clientconst oauth = auth.initOauth();// Get an access token for the userconst token = await oauth.getAccessToken();// If a token isn't available, the user isn't authorizedif (token) {// The user is authorizedconsole.log(token);} else {console.log("The user is not authorized.");}