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}}
ts
Returns
A client for authorizing users with OAuth.
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
Optional
Options for requesting an access token for the current user.
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:
The access token for the current user.
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.");}
ts