Skip to main content
Before this: create and initialize a Dynamic client (see Creating a Dynamic Client, Initializing the Dynamic Client). Here, user means the person using your app; Dynamic user is the object created after they authenticate. A user can be signed in (e.g. wallet connected) without yet having a Dynamic user. See Signing in with a Wallet for more.

Check if signed in

import { createDynamicClient, isSignedIn } from '@dynamic-labs-sdk/client';

const dynamicClient = createDynamicClient({
  environmentId: 'YOUR_ENVIRONMENT_ID',
  metadata: {
    name: 'YOUR_APP_NAME',
    url: 'YOUR_APP_URL',
    iconUrl: 'YOUR_APP_ICON_URL',
  },
});

// ...

// If the user is authenticated (Dynamic user exists) OR has a connected wallet to the session (no Dynamic user exists), the function will return true
const isUserSignedIn = isSignedIn();
console.log(isUserSignedIn);

Log the user out

Call the logout function, and it will clear all the session data, including the Dynamic user and any connected wallets (non verified wallets).
import { logout } from '@dynamic-labs-sdk/client';

const handleLogout = async () => {
  await logout();
};

// ...

Get the current authenticated user


// If the user is authenticated, the user object will be available in the dynamicClient.user property
const user = dynamicClient.user;
console.log(user);

Get the user JWT


// If the user is authenticated and you are not using cookies authentication, the jwt will be available in the dynamicClient.token property
const jwt = dynamicClient.token;
console.log(jwt);

Update the current authenticated user

Call updateUser to update the current Dynamic user. You can pass many user fields to the function, and it will update the user with the new values. If any of the fields require OTP verification (like email or phone number), the function will return an OTPVerification object, that you can use to check what kind of OTP verification is required (email or phone number), send the OTP to the user, and verify the OTP.
import {
  sendEmailOTP,
  updateUser,
  verifyOTP
} from '@dynamic-labs-sdk/client';

const handleUpdateUser = async (newName, newEmail) => {
  const otpVerification = await updateUser({ name: newName, email: newEmail });
  console.log(otpVerification);

  if (!otpVerification) {
    return;
  }

  if (otpVerification?.email) {
    // This call will send the OTP to the user's email, you should then collect the OTP from the user and then call the verifyOTP function
    await sendEmailOTP({ email: newEmail });
  }
};

const handleVerifyOTP = async (otpVerification, otpCode) => {
  // Use the same otpVerification object that you got from the updateUser function
  await verifyOTP({ otpVerification, otp: otpCode });
};

// ...

Check if a user has missing fields

You can toggle user data collected and also to be required or not in the Dynamic dashboard. We don’t control that in the JavaScript SDK, but you can check if a user has missing fields by calling the user.missingFields property. With that, you can display the appropriate UI to the user to complete the onboarding process, and call the updateUser function to update the user with the missing fields.

const onUserAuthenticated = async () => {
  const { missingFields } = dynamicClient.user;

  if (!missingFields.length) {
    // User has no missing fields
    return;
  }

  missingFields.forEach((field) => {
    console.log(`Name : ${field.name}`);
    // If you just want to know the required fields, you can check the field.required property
    console.log(`Required : ${field.required}`);
    console.log(`Type : ${field.type}`);
    // You can check if OTP verification is required for this field by checking the field.verification property
    console.log(`Type : ${field.verification}`);
  });
};

// ...

Check if onboarding is complete

You can check if a user has completed all onboarding requirements (including KYC fields, MFA authentication, and recovery codes acknowledgment) by calling the isUserOnboardingComplete function. This function provides a comprehensive check of all requirements and returns true only when the user has completed everything.
import { isUserOnboardingComplete } from '@dynamic-labs-sdk/client';

const onUserAuthenticated = () => {
  const isComplete = isUserOnboardingComplete();

  if (!isComplete) {
    // User has pending onboarding requirements
    // You can check individual requirements using:
    // - user.missingFields for KYC fields
    // - isUserMissingMfaAuth() for MFA authentication
    // - isPendingRecoveryCodesAcknowledgment() for recovery codes
    redirectToOnboarding();
  } else {
    // User has completed all requirements
    redirectToDashboard();
  }
};

// ...

See the isUserOnboardingComplete documentation for more details on how to use this function and check individual requirements.

Refreshing the current authenticated user (Dynamic user)

You can refresh the current authenticated user (Dynamic user) by calling the refreshUser function. This function will refresh the user object in the SDK with the latest data from the server.
import { refreshUser } from '@dynamic-labs-sdk/client';

const handleRefreshUser = async () => {
  await refreshUser();
};

// ...