Authentication API

A quick introduction to the Authentication API in Makerkit

To check if users are authed, or to retrieve information about the currently signed-in user, use the requireUser function:

import { redirect } from 'next/navigation'; import { requireUser } from '@kit/supabase/require-user'; import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client'; async function ServerComponent() { const client = getSupabaseServerComponentClient(); const auth = await requireUser(client); // check if the user needs redirect if (auth.error) { redirect(auth.redirectTo); } // user is authed! const user = auth.data; }

NB: use the correct Supabase client based on the context. In this case, we use the server component client.

'use server'; import { redirect } from 'next/navigation'; import { requireUser } from '@kit/supabase/require-user'; import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client'; export async function myServerAction() { const client = getSupabaseServerActionClient(); const auth = await requireUser(client); // check if the user needs redirect if (auth.error) { redirect(auth.redirectTo); } // user is authed! const user = auth.data; }

If the user needs MFA and is not yet verified, the redirect function will redirect the user to the MFA verification page. This is why it is important to check the redirectTo property in the response.


Subscribe to our Newsletter
Get the latest updates about React, Remix, Next.js, Firebase, Supabase and Tailwind CSS