ESC
Features > Magic Link
Setup
1. Supabase manages Magic Links on your behalf automatically.
2. You can directly start authenticating your users on frontend side just like the code sample below:
/app/components/SignInButton.ts
1
2 const handleOtpSignup = async (options: {
3 type: string;
4 provider?: Provider;
5 }) => {
6 setIsLoading(true);
7
8 try {
9 const { type, provider } = options;
10 const redirectURL = window.location.origin + "/api/auth/callback";
11
12 await supabase.auth.signInWithOtp({
13 email,
14 options: {
15 emailRedirectTo: redirectURL,
16 },
17 });
18 toast.success("Check your emails!");
19 } catch (error) {
20 console.log(error);
21 } finally {
22 setIsLoading(false);
23 }
24 };
3. When you need to check the authentication status of your users on backend, you can do it like the code below:
/app/api/example/route.ts
1
2 import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
3 import { NextResponse } from "next/server";
4 import { cookies } from "next/headers";
5
6 export const dynamic = "force-dynamic";
7
8 export async function POST(req) {
9 const supabase = createRouteHandlerClient({ cookies });
10 const { data } = await supabase.auth.getSession();
11 const { session } = data;
12
13 // Check authentication by the object returned by Supabase
14 if (session) {
15 const body = await req.json();
16 // you can do what you need to do with the authenticated user
17 } else {
18 // Unauthenticated
19 NextResponse.json({ error: "Not signed in" }, { status: 401 });
20 }
21 }