For the complete documentation index, see llms.txt. This page is also available as Markdown.

Link and Unlink Social/Enterprise Provider with the SDK

Let signed-in users connect a social or enterprise login provider to their account, and disconnect it later, from your own UI. This guide uses Google as the example provider, but the same code works for any OAuth provider you have configured.

These SDK methods are currently available in the web SDK (@authgear/web) only. For other SDKs, use the User Settings page and users can manage their Social/Enterprise connection there.

What you will build

A page in your app, such as an account details page, that:

  1. Reads the signed-in user's linked identities.

  2. Shows a Link button when the provider is not connected, or an Unlink button when it is.

  3. Links the provider by redirecting to Google's consent screen, then back to your app.

  4. Unlinks the provider by redirecting to an Authgear-hosted confirmation page, then back to your app.

Both actions use a browser redirect. The user leaves your app, completes the step on Google's or Authgear's page, and returns to a redirect URI you control. Your callback code calls a finish method to complete the action.

Prerequisites

  • The @authgear/web SDK installed and configured in your app. See Getting Started.

  • The user is already signed in. Linking and unlinking require an authenticated session. Both methods throw if no valid session exists.

  • The OAuth provider is configured in the Authgear Portal under Social / Enterprise Login, with an alias. This guide uses the alias google.

  • The redirect URI you pass to each method is registered under your application's Redirect URIs in the Portal.

The examples assume your app configures the SDK with sessionType: "refresh_token".

Step 1: Check whether the provider is linked

Call fetchUserInfo() to get the user's info, then inspect the identities array. Each OAuth identity has type === IdentityType.OAuth and an oauthProviderAlias that matches the alias you configured in the Portal.

The raw identities claim from the UserInfo endpoint looks like this:

Render the button that matches the current state.

Call startLinkOAuth(). The SDK redirects the browser to Google's consent screen, where the user signs in and approves the link. After approval, Google redirects back to your redirectURI.

Option
Type
Required
Description

oauthProviderAlias

string

Yes

The provider alias configured in the Portal, e.g. google.

redirectURI

string

Yes

Where the browser returns after the flow. Must be registered in the Portal.

state

string

No

An OAuth state value returned to your callback. You can use it to tell link, unlink, and sign-in callbacks apart.

Call startUnlinkOAuth(). The SDK redirects the browser to an Authgear-hosted page with an unlink button. The user confirms there, and Authgear redirects back to your redirectURI.

The options are the same as startLinkOAuth(). Use a different state value so your callback knows which action to finish.

Step 5: Handle the callback

When the user returns to your redirectURI, finish the action. Read the state query parameter to choose between finishLinkOAuth() and finishUnlinkOAuth(), then send the user back to your account details page.

Last updated

Was this helpful?