Complete the code to check if a user is logged in using Supabase client.
const user = supabase.auth.[1]();The supabase.auth.session() method returns the current session, which includes the user if logged in.
Complete the code to redirect unauthenticated users to the login page.
if (!user) { window.[1] = '/login'; }
Setting window.location changes the current page URL, redirecting the user.
Fix the error in the code to protect a route by checking user session.
const session = supabase.auth.[1](); if (!session) { redirect('/login'); }
The correct method is supabase.auth.session() to get the current session.
Fill both blanks to create a React hook that redirects if no user is logged in.
import { useEffect } from 'react'; import { useRouter } from 'next/router'; function useProtectedRoute() { const router = useRouter(); const session = supabase.auth.[1](); useEffect(() => { if (!session) { router.[2]('/login'); } }, [session]); }
supabase.auth.session() gets the current session including user info. router.push('/login') redirects to login page.
Fill all three blanks to create a Next.js API route that checks authentication before responding.
export default async function handler(req, res) {
const { data: { user } } = await supabase.auth.api.[1](req.headers.cookie);
if (!user) {
return res.status([2]).json({ message: '[3]' });
}
res.status(200).json({ message: 'Welcome!' });
}supabase.auth.api.getUserByCookie() extracts user from cookies. Status code 401 means unauthorized access. The message 'Unauthorized' informs the client.