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 { getSupabaseServerClient } from '@kit/supabase/server-client';async function ServerComponent() { const client = getSupabaseServerClient(); 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 { getSupabaseServerClient } from '@kit/supabase/server-client';export async function myServerAction() { const client = getSupabaseServerClient(); 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.