Supabase provides a way to keep users logged in across page reloads. What is the main mechanism Supabase uses to persist user sessions in the frontend?
Think about where browsers commonly store data that survives page reloads.
Supabase automatically stores the session token in local storage, allowing the user to stay logged in even after refreshing the page. This is handled internally by the Supabase client.
You want to protect a frontend route so only logged-in users can access it. Which approach works best with Supabase's client library?
Think about how to verify user login status before showing protected content.
Using supabase.auth.getSession() lets you check if the user is logged in on the client side before rendering protected routes, preventing unauthorized access.
In a React app using Supabase, you want to redirect users who are not logged in to the login page when they try to access a protected route. Which code snippet correctly implements this behavior?
import { useEffect } from 'react'; import { useRouter } from 'next/router'; import { supabase } from '../utils/supabaseClient'; function ProtectedPage() { const router = useRouter(); useEffect(() => { async function checkAuth() { const session = await supabase.auth.getSession(); if (!session.data.session) { router.push('/login'); } } checkAuth(); }, []); return <div>Protected Content</div>; }
Check the structure of the object returned by supabase.auth.getSession().
The getSession() method returns an object with a data property containing the session. So you must check session.data.session to see if a user is logged in.
If you protect routes only by checking user login status on the frontend with Supabase, what is the main security risk?
Think about what users can do with their browsers and client code.
Frontend checks can be bypassed by users who disable JavaScript or modify the code, so backend security rules and API protections are necessary to truly secure data.
Supabase supports real-time data subscriptions. When using protected routes, what must you ensure about these subscriptions to maintain security?
Consider when and how subscriptions connect to the backend.
Real-time subscriptions should only start after verifying the user is authenticated, otherwise unauthorized users might receive sensitive data streams.