Skip to content
LogoLogo

Linking & unlinking accounts

Developers can use Openfort to prompt users to link additional accounts (such as a wallet, phone number, or social profile) at any point in their user journey, not just during login.

Linking accounts

Add an email address to an existing account. Use this to upgrade guest users or add email as a secondary authentication method:

import openfort from "./openfortConfig"
 
async function addEmail(email: string) {
  const result = await openfort.auth.addEmail({
    email: email,
    callbackURL: 'https://your-app.com/verify-email',
  });
 
  console.log('Verification email sent');
}

Initialize an OAuth linking process. Returns the OAuth authorization URL:

import { OAuthProvider } from '@openfort/openfort-js';
import openfort from "./openfortConfig"
 
async function initLinkOAuth(provider: OAuthProvider) {
  const url = await openfort.auth.initLinkOAuth({
    provider: provider,
    redirectTo: 'https://your-app.com/auth/callback',
  });
 
  window.location.href = url;
}

Link a wallet using SIWE. First initialize the SIWE challenge to get a nonce, then create and sign the SIWE message, and finally link with the signature:

Step 1: Initialize SIWE challenge

import openfort from "./openfortConfig"
 
async function initLinkSIWE(address: string) {
  const { nonce } = await openfort.auth.initLinkSiwe({ address });
 
  // Use this nonce when creating your SIWE message
  return nonce;
}
import openfort from "./openfortConfig"
 
async function linkWallet(
  signature: string,
  message: string,
  address: string,
  chainId: number,
  walletClientType: string,
  connectorType: string
) {
  await openfort.auth.linkWithSiwe({
    signature: signature,
    message: message,
    address: address,
    chainId: chainId,
    walletClientType: walletClientType,
    connectorType: connectorType,
  });
 
  console.log('Wallet linked successfully');
}

Link a phone number using SMS OTP. First request the OTP, then verify:

import openfort from "./openfortConfig"
 
async function requestPhoneOtp(phoneNumber: string) {
  await openfort.auth.requestPhoneOtp({ phoneNumber });
  console.log('OTP sent to phone');
}
 
async function linkPhone(phoneNumber: string, otp: string) {
  const result = await openfort.auth.linkPhoneOtp({
    phoneNumber: phoneNumber,
    otp: otp,
  });
 
  console.log('Phone linked:', result.user.id);
}

Unlinking accounts

Allow users to unlink accounts they have previously linked.

Unlink an OAuth provider from the account:

import { OAuthProvider } from '@openfort/openfort-js';
import openfort from "./openfortConfig"
 
async function unlinkOAuth(provider: OAuthProvider) {
  await openfort.auth.unlinkOAuth({ provider });
 
  console.log('Account unlinked');
}

Unlink a wallet from the account:

import openfort from "./openfortConfig"
 
async function unlinkWallet(address: string, chainId: number) {
  await openfort.auth.unlinkWallet({
    address: address,
    chainId: chainId,
  });
 
  console.log('Wallet unlinked');
}