Allow users to authenticate with a social provider, like Apple, Google, Facebook, X, etc.

Prerequisites

  • You need to have the Dynamic Client initialized.
  • You need to have the required providers enabled and configured correctly in your environment’s settings in the Dynamic dashboard.

Usage

import {
  completeSocialRedirectSignIn,
  detectOAuthRedirect,
  type SocialProvider,
  signInWithSocialRedirect,
} from '@dynamic-labs-sdk/client';

// First, call signInWithSocialRedirect to redirect the user to the social provider's authorization page
// The redirectUrl should be the URL of your app that the user will be redirected to after they authenticate with the social provider
// It does not need to be a specific page, it can be the root URL of your app
const singInWithSocial = async (provider: SocialProvider) => {
  await signInWithSocialRedirect({
    provider,
    redirectUrl: 'https://your-app.com/callback',
  });
}

// Use the `detectOAuthRedirect` helper function when you app loads to check if the user is returning from the social provider's authorization page
// If the user is returning, use `completeSocialRedirectSignIn` to complete the authentication process
const detectRedirect = async () => {
  const currentUrl = new URL(window.location.href);
  const isReturning = await detectOAuthRedirect({
    url: currentUrl,
  });

  if (isReturning) {
    await completeSocialRedirectSignIn({
      url: currentUrl,
    });

    // User is now authenticated
  }
}

Available providers

User data

When a user authenticates with a social provider, the user data is stored in the user object. If the social provider returns an email address, it will be added to the user’s emails list and can be used to authenticate with email.