Nextbase Docs
Nextbase Docs
HomeBlogWelcome to Nextbase v3!Getting Started with NextBaseQuick setup
OverviewLogin with PasswordLogin with Social ProvidersLogin with Magic LinkLogin with API Key
Authentication

Login with Magic Link

Nextbase supports login with Magic Link, which is a secure way to authenticate users without requiring them to remember a password.

Server Action

Here's the server action used to login with Magic Link:

"use server";
import { createSupabaseUserServerActionClient } from "@/supabase-clients/user/createSupabaseUserServerActionClient";
import { toSiteURL } from "@/utils/helpers";

export const signInWithMagicLink = async (email: string, next?: string) => {
  const supabase = createSupabaseUserServerActionClient();
  const redirectUrl = new URL(toSiteURL("/auth/callback"));
  if (next) {
    redirectUrl.searchParams.set("next", next);
  }
  const { error } = await supabase.auth.signInWithOtp({
    email,
    options: {
      emailRedirectTo: redirectUrl.toString(),
    },
  });

  if (error) {
    console.log(error);
    throw error;
  }
};

Usage

Here's an example of how to use the Magic Link authentication in your login component:

const magicLinkMutation = useToastMutation(
  async (email: string) => {
    return await signInWithMagicLink(email, next);
  },
  {
    loadingMessage: "Sending magic link...",
    errorMessage: "Failed to send magic link",
    successMessage: "Magic link sent!",
    onSuccess: () => {
      setSuccessMessage("A magic link has been sent to your email!");
    },
    onError: (error) => {
      console.log(error);
    },
    onMutate: () => {
      setSuccessMessage(null);
    },
  },
);

// In your component JSX
<Email
  onSubmit={magicLinkMutation.mutate}
  isLoading={magicLinkMutation.isLoading}
  view="sign-in"
/>;

This implementation allows users to receive a login link via email, providing a passwordless authentication experience.

Login with Social Providers

Nextbase supports authentication with various social providers, allowing users to log in using their existing accounts from platforms like Google, GitHub, and Twitter.

Login with API Key

Nextbase Ultimate supports authentication using API keys, which is useful for integrating with external services or for machine-to-machine communication.

On this page

Server ActionUsage