For the sake of this documentation, we will refer to the user as the user that is using the application, and Dynamic user as the user object that gets created after authentication. So a user can be signed in, but not have a Dynamic user if they have not authenticated (i.e. if they sign in with a wallet connection only). (See Signing in with a Wallet for more information.)

Checking if the user is 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);

Logging out

Logging out is done by calling 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();
};

// ...

Getting the current authenticated user (Dynamic user)


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

Getting 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);

Updating the current authenticated user (Dynamic user)

You can update the current authenticated user (Dynamic user) by calling the updateUser function. 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(response);

  if (!otpVerification) {
    return;
  }

  if (otpVerification?.email) {
    // This call will send the OTP to the user's email, you should them 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, verificationToken: otpCode });
};

// ...

Checking 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;
  }

  user.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}`);
  });
};

// ...

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();
};

// ...