Skip to content

Email Magic Link Authentication

Email magic link authentication is a two-step process:

  1. The user enters their email address and requests a magic link
  2. The user clicks the link in their email, which redirects them back to your application to complete authentication

You can implement Email Magic Link authentication in two ways:

Pre-built UI Components

Account Kit provides pre-built UI components that handle the entire Email Magic Link authentication flow with minimal code.

Step 1: Add Authentication Components to Your Page

Before configuring your authentication, first add one of the pre-built components to your application:

Using Modal Authentication

To add authentication in a modal popup:

import React from "react";
import { useAuthModal } from "@account-kit/react";
 
export default function MyPage() {
  const { openAuthModal } = useAuthModal();
 
  return <button onClick={openAuthModal}>Sign in</button>;
}

For more details on modal configuration, see the Modal Authentication documentation.

Or:

Using Embedded Authentication

To embed authentication directly in your page:

import React from "react";
import { AuthCard } from "@account-kit/react";
 
export default function MyLoginPage() {
  return (
    <div className="flex flex-row p-4 bg-white border border-gray-200 rounded-lg">
      <AuthCard />
    </div>
  );
}

For more details on embedded authentication, see the Embedded Authentication documentation.

Step 2: Configure Email Magic Link in UI Components

After adding the components, configure the Email Magic Link authentication in your application config:

import { AlchemyAccountsUIConfig, createConfig } from "@account-kit/react";
import { sepolia, alchemy } from "@account-kit/infra";
 
const uiConfig: AlchemyAccountsUIConfig = {
  auth: {
    sections: [
      [
        {
          type: "email",
          emailMode: "magicLink",
 
          // Optional customizations:
          buttonLabel: "Continue with Email",
          placeholder: "Enter your email address",
        },
      ],
    ],
  },
};
 
export const config = createConfig(
  {
    transport: alchemy({ apiKey: "your-api-key" }),
    chain: sepolia,
  },
  uiConfig
);

Email Magic Link configuration accepts the following options:

type EmailAuthType = {
  type: "email";
  emailMode: "magicLink";
  // hides the continue button
  hideButton?: boolean;
  // changes the button label
  buttonLabel?: string;
  // changes the placeholder text in the input
  placeholder?: string;
};

You can find the full type definition in the Account Kit source code.

For more details on UI component customization, see the UI Components documentation.

Custom UI

If you need complete control over the user experience, you can implement your own custom UI for Email Magic Link authentication using Account Kit hooks.

Step 1: Send the Magic Link

First, prompt your user for their email address and send a magic link:

import { useAuthenticate } from "@account-kit/react";
 
// Inside your component
const { authenticate } = useAuthenticate();
 
// When the user submits their email
const handleSendMagicLink = (email: string) => {
  authenticate(
    {
      type: "email",
      emailMode: "magicLink",
      email,
    },
    {
      onSuccess: () => {
        // Success - inform user to check their email
      },
      onError: (error) => {
        // Handle error
      },
    }
  );
};

Step 2: Handle the Redirect

When the user clicks the magic link in their email, they'll be redirected back to your application. You need to extract the authentication bundle from the URL and complete the authentication:

import { useEffect } from "react";
import { useAuthenticate } from "@account-kit/react";
 
// Inside your component
const { authenticate } = useAuthenticate();
 
// Handle the redirect when the component mounts
useEffect(() => {
  const handleRedirect = () => {
    const url = new URL(window.location.href);
    const bundle = url.searchParams.get("bundle");
 
    if (bundle) {
      authenticate(
        {
          type: "email",
          bundle,
        },
        {
          onSuccess: () => {
            // Authentication successful!
          },
          onError: (error) => {
            // Handle error
          },
        }
      );
    }
  };
 
  handleRedirect();
}, [authenticate]);

Step 3: Track Authentication Status

Use the useSignerStatus hook to determine if the user is authenticated:

import { useSignerStatus } from "@account-kit/react";
 
// Inside your component
const { isConnected } = useSignerStatus();
 
// You can use isConnected to conditionally render UI