ConnectedAccount
OAuthConnection represents an OAuth connection to an external provider (like Google, GitHub, etc.) that is linked to a user. You can use connected accounts to access the user's data on those platforms, such as reading Google Drive files or sending emails via Gmail.
For a guide on how to use connected accounts, see the OAuth guide.
On this page:
Connection
Basic information about a connected account. This is the base type that OAuthConnection extends.
Table of Contents
The provider config ID. This is the same as provider and exists for backward compatibility.
declare const id: string;The provider config ID (e.g., "google", "github").
declare const provider: string;The account ID from the OAuth provider (e.g., the Google user ID).
declare const providerAccountId: string;OAuthConnection
Extends Connection with methods to retrieve OAuth access tokens. Get it with:
user.getConnectedAccount({ provider, providerAccountId })user.useConnectedAccount({ provider, providerAccountId })user.listConnectedAccounts()user.useConnectedAccounts()user.getOrLinkConnectedAccount(provider)user.useOrLinkConnectedAccount(provider)
Table of Contents
Gets an OAuth access token for this connected account. The token can be used to call the provider's APIs on the user's behalf.
Returns a Result object:
- On success:
{ status: "ok", data: { accessToken: string } } - On error:
{ status: "error", error: OAuthAccessTokenNotAvailable }if the refresh token has been revoked/expired or the requested scopes are not available.
Parameters
optionsobjectShow Properties
scopesstring[]If provided, only returns a token that has all of these scopes. If the current token doesn't have the required scopes, the result will be an error.
Returns
Promise<Result<{ accessToken: string }, OAuthAccessTokenNotAvailable>>
declare function getAccessToken(options?: {
scopes?: string[];
}): Promise<Result<
{ accessToken: string },
OAuthAccessTokenNotAvailable
>>;const result = await account.getAccessToken();
if (result.status === "ok") {
const { accessToken } = result.data;
// Use accessToken to call provider APIs
}const result = await account.getAccessToken({
scopes: ["https://www.googleapis.com/auth/drive.readonly"],
});React hook version of getAccessToken. Returns the access token result reactively.
Returns a Result object:
- On success:
{ status: "ok", data: { accessToken: string } } - On error:
{ status: "error", error: OAuthAccessTokenNotAvailable }if the refresh token has been revoked/expired or the requested scopes are not available.
Parameters
optionsobjectShow Properties
scopesstring[]If provided, only returns a token that has all of these scopes.
Returns
Result<{ accessToken: string }, OAuthAccessTokenNotAvailable>
declare function useAccessToken(options?: {
scopes?: string[];
}): Result<
{ accessToken: string },
OAuthAccessTokenNotAvailable
>;function MyComponent() {
const user = useUser({ or: "redirect" });
const accounts = user.useConnectedAccounts();
const googleAccount = accounts.find(
a => a.provider === "google"
);
const result = googleAccount?.useAccessToken();
if (result?.status === "ok") {
return <div>Token: {result.data.accessToken}</div>;
}
return <div>No Google token available</div>;
}