Authentication helps confirm who you are when using a website or app. It keeps your information safe and private.
Why authentication matters in NextJS
import { useSession, signIn, signOut } from 'next-auth/react'; function Component() { const { data: session } = useSession(); if (session) { return ( <> <p>Welcome, {session.user.name}!</p> <button onClick={() => signOut()}>Sign out</button> </> ); } return <button onClick={() => signIn()}>Sign in</button>; }
This example uses next-auth, a popular authentication library for Next.js.
useSession hook checks if the user is logged in or not.
import { useSession } from 'next-auth/react'; function Greeting() { const { data: session } = useSession(); return <p>{session ? `Hello, ${session.user.email}` : 'Please sign in'}</p>; }
import { signIn } from 'next-auth/react'; export default function SignInButton() { return <button onClick={() => signIn('github')}>Sign in with GitHub</button>; }
This component shows a welcome message and sign out button if the user is logged in. Otherwise, it shows a sign in button.
import { useSession, signIn, signOut } from 'next-auth/react'; export default function AuthExample() { const { data: session } = useSession(); if (session) { return ( <main> <h1>Welcome, {session.user.name}!</h1> <p>You are signed in.</p> <button onClick={() => signOut()}>Sign out</button> </main> ); } return ( <main> <h1>You are not signed in.</h1> <button onClick={() => signIn()}>Sign in</button> </main> ); }
Always protect sensitive pages by checking authentication on the server or client.
Use secure cookies and HTTPS to keep user data safe.
Make sure to handle loading states when checking if a user is signed in.
Authentication confirms who a user is and protects their data.
Next.js apps use libraries like next-auth to handle authentication easily.
Always check if a user is signed in before showing private content.
Practice
Solution
Step 1: Understand the purpose of authentication
Authentication is used to confirm who a user is when they access an app.Step 2: Recognize the importance of protecting data
It helps protect private or sensitive data by allowing only authorized users to see it.Final Answer:
It confirms the identity of users and protects private data. -> Option CQuick Check:
Authentication = Confirm identity and protect data [OK]
- Confusing authentication with app speed
- Thinking authentication changes UI colors
- Believing authentication fixes code bugs
Solution
Step 1: Identify the ES module import syntax
Next.js uses ES module syntax withimportto load libraries.Step 2: Match the correct import statement
The correct import isimport NextAuth from 'next-auth';with exact casing and syntax.Final Answer:
import NextAuth from 'next-auth'; -> Option DQuick Check:
Use ES module import syntax for NextAuth [OK]
- Using CommonJS require instead of import
- Wrong casing in import statement
- Using include which is not valid in JS
import { useSession } from 'next-auth/react';
export default function Profile() {
const { data: session } = useSession();
if (!session) {
return <p>Please sign in to view your profile.</p>;
}
return <p>Welcome, {session.user.name}!</p>;
}Solution
Step 1: Check the session state when user is not signed in
If the user is not signed in,sessionwill benullorundefined.Step 2: Follow the conditional rendering logic
The code returns the message<p>Please sign in to view your profile.</p>when!sessionis true.Final Answer:
Please sign in to view your profile. -> Option AQuick Check:
Not signed in = show sign-in prompt [OK]
- Assuming user name shows without sign-in
- Expecting loading text instead of sign-in prompt
- Thinking an error will be thrown
import { useSession } from 'next-auth/react';
export default function Dashboard() {
const session = useSession();
if (!session) {
return <p>Access denied.</p>;
}
return <p>Dashboard content</p>;
}Solution
Step 1: Check how useSession is used
useSession returns an object with adataproperty containing the session info.Step 2: Identify the correct destructuring
The code should useconst { data: session } = useSession();to get the session data.Final Answer:
useSession must be destructured to get data property. -> Option BQuick Check:
Destructure useSession to access session data [OK]
- Using useSession without destructuring
- Trying to put return inside useEffect
- Thinking class components are required
- Using var instead of const or let
Solution
Step 1: Understand server-side protection
UsinggetServerSidePropsallows checking the session before rendering the page.Step 2: Redirect unauthorized users
If no session is found, redirecting to sign-in page prevents unauthorized access securely.Final Answer:
Use getServerSideProps to check session and redirect if not signed in. -> Option AQuick Check:
Server-side session check = secure page protection [OK]
- Hiding content with CSS does not secure data
- Using client-side delay risks exposing content
- Allowing access and just showing alerts is insecure
Ready to prove it?
Pass the test and this pattern moves unknown
