What is a protected route in a frontend application?
A protected route is a page or section in a frontend app that only logged-in users can access. It helps keep private info safe by blocking users who are not signed in.
Click to reveal answer
beginner
How does Supabase help manage protected routes?
Supabase provides user authentication and session management. It lets the frontend check if a user is logged in before showing protected pages.
Click to reveal answer
beginner
What happens if a user tries to access a protected route without being logged in?
The app usually redirects the user to a login page or shows a message asking them to sign in first.
Click to reveal answer
intermediate
Why is it important to check authentication status on the frontend for protected routes?
Checking on the frontend improves user experience by quickly blocking access to private pages and guiding users to log in without waiting for server responses.
Click to reveal answer
intermediate
Name a simple way to implement protected routes in a frontend app using Supabase.
Use Supabase's auth client to check if a user session exists. If yes, show the protected page; if no, redirect to login.
Click to reveal answer
What does a protected route require before allowing access?
AUser must be logged in
BUser must have admin rights
CUser must be offline
DUser must clear browser cache
✗ Incorrect
Protected routes require the user to be logged in to access private content.
Which Supabase feature helps verify if a user is logged in?
ASupabase Storage
BSupabase Auth client
CSupabase Realtime
DSupabase Functions
✗ Incorrect
Supabase Auth client manages user sessions and authentication status.
If a user is not logged in and tries to access a protected route, what should happen?
ALog the user out
BShow the protected content anyway
CReload the page
DRedirect to login page
✗ Incorrect
Users not logged in should be redirected to login to protect private content.
Why check authentication on the frontend for protected routes?
ATo improve user experience by blocking access quickly
BTo slow down the app
CTo avoid using Supabase
DTo allow anonymous access
✗ Incorrect
Frontend checks help block unauthorized access fast and guide users properly.
What is a common method to protect routes in a frontend app using Supabase?
AUse CSS to hide content
BStore passwords in local storage
CCheck user session with Supabase Auth before rendering
DDisable JavaScript
✗ Incorrect
Checking user session before showing content ensures only logged-in users access protected routes.
Explain how you would implement a protected route in a frontend app using Supabase.
Think about how to verify login status and control page access.
You got /3 concepts.
Why is it important to protect routes on the frontend, and how does it improve user experience?
Consider both security and user flow benefits.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of protected routes in a frontend application using Supabase?
easy
A. To allow only logged-in users to access certain pages
B. To speed up page loading times
C. To store user data permanently
D. To change the app's theme automatically
Solution
Step 1: Understand what protected routes do
Protected routes restrict access to certain pages so only authorized users can see them.
Step 2: Connect with Supabase session usage
Supabase sessions help check if a user is logged in, enabling route protection.
Final Answer:
To allow only logged-in users to access certain pages -> Option A
Quick Check:
Protected routes = restrict access to logged-in users [OK]
Hint: Protected routes block access unless user is logged in [OK]
Common Mistakes:
Thinking protected routes speed up loading
Confusing route protection with data storage
Assuming protected routes change app appearance
2. Which of the following is the correct way to check if a user is logged in using Supabase in frontend code?
easy
A. const user = supabase.auth.getUser()
B. const user = supabase.session()
C. const user = supabase.auth.session()
D. const user = supabase.auth.getSession()
Solution
Step 1: Identify Supabase method for session retrieval
Supabase's current method to get session info is auth.getSession(), which returns user session data.
Step 2: Compare options with official API
Options A, B, and C are outdated or incorrect method names; only getSession() is valid.
Final Answer:
const user = supabase.auth.getSession() -> Option D
Quick Check:
Use auth.getSession() to check login [OK]
Hint: Use supabase.auth.getSession() to get current user session [OK]
Common Mistakes:
Using deprecated methods like auth.session()
Calling supabase.session() which does not exist
Confusing getUser() with getSession()
3. Given this React snippet using Supabase, what will happen if the user is not logged in?
B. An error is thrown because getSession() is async
C. The user is redirected to the login page
D. Nothing happens; the page stays the same
Solution
Step 1: Understand getSession() behavior
supabase.auth.getSession() is an asynchronous function returning a Promise, so calling it without await assigns a Promise object to session, not session data.
Step 2: Analyze the code flow
A Promise object is truthy, so if (!session) evaluates to false, and showDashboard() executes regardless of login status.
Final Answer:
The dashboard is shown regardless -> Option A
Quick Check:
Async getSession() without await returns truthy Promise [OK]
Hint: Remember to await async getSession() calls [OK]
Common Mistakes:
Treating async getSession() as synchronous
Assuming session is null if user not logged in
Ignoring Promise returned by getSession()
4. You wrote this code to protect a route but it redirects even logged-in users to login:
A. session is always truthy even if user is logged out
B. window.location.href cannot redirect pages
C. getSession() is asynchronous and must be awaited
D. You must use supabase.auth.getUser() instead
Solution
Step 1: Check getSession() usage
supabase.auth.getSession() returns a Promise and must be awaited or handled asynchronously to get session data.
Step 2: Understand destructuring without await
Destructuring data from a Promise without await results in undefined or incorrect values, so session is not properly checked.
Final Answer:
getSession() is asynchronous and must be awaited -> Option C
Quick Check:
Always await async getSession() calls [OK]
Hint: Always await async Supabase calls to get real data [OK]
Common Mistakes:
Not awaiting async functions
Thinking window.location.href can't redirect
Using getUser() instead of getSession() incorrectly
5. You want to protect a React page so only logged-in users can see it. Which approach correctly implements this using Supabase's session and React hooks?
function ProtectedPage() {
const [session, setSession] = React.useState(null);
React.useEffect(() => {
const getSession = async () => {
const { data } = await supabase.auth.getSession();
setSession(data.session);
if (!data.session) {
navigate('/login');
}
};
getSession();
}, []);
if (!session) return <p>Loading...</p>;
return <div>Welcome to protected content!</div>;
}
hard
A. The navigate call should be outside the async function
B. This code correctly protects the page by redirecting unauthenticated users
C. The code should call supabase.auth.getUser() instead of getSession()
D. The session state should be initialized with supabase.auth.getSession() synchronously
Solution
Step 1: Analyze async session retrieval in useEffect
The code correctly uses an async function inside useEffect to get the session and update state.
Step 2: Check redirect logic for unauthenticated users
If no session exists, the code calls navigate('/login') to redirect users, protecting the page.
Step 3: Confirm loading state handling
While session is null, it shows a loading message, preventing content flash before redirect.
Final Answer:
This code correctly protects the page by redirecting unauthenticated users -> Option B