Stack Auth
WelcomeGuidesSDK
ComponentsAPI Reference
SDK Overview
Objects
StackApp
Types
UserTeamTeamUserTeamPermissionTeamProfileContactChannelEmailApiKeyProjectConnectedAccountItemCustomer
Hooks
useStackAppuseUser
Stack Auth Docs

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
  • OAuthConnection

Connection

Basic information about a connected account. This is the base type that OAuthConnection extends.

Table of Contents

Connection Table of Contents

The provider config ID. This is the same as provider and exists for backward compatibility.

Type Definition
declare const id: string;

The provider config ID (e.g., "google", "github").

Type Definition
declare const provider: string;

The account ID from the OAuth provider (e.g., the Google user ID).

Type Definition
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

OAuthConnection 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

optionsobject
Show 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>>

Signature
declare function getAccessToken(options?: {
  scopes?: string[];
}): Promise<Result<
  { accessToken: string },
  OAuthAccessTokenNotAvailable
>>;
Examples
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

optionsobject
Show Properties
scopesstring[]

If provided, only returns a token that has all of these scopes.

Returns

Result<{ accessToken: string }, OAuthAccessTokenNotAvailable>

Signature
declare function useAccessToken(options?: {
  scopes?: string[];
}): Result<
  { accessToken: string },
  OAuthAccessTokenNotAvailable
>;
Examples
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>;
}
Previous PageProject
Next PageItem

Stack Auth AI

Experimental: AI responses may not always be accurate—please verify important details.

For the most accurate information, please join our Discord or email us.

How can I help?

Ask me about Stack Auth while you browse the docs.