How To Authenticate Users in Nextjs With NextAuth App Router VS Pages Router
How To Authenticate Users in Nextjs With NextAuth App Router VS Pages Router
How To Authenticate Users in Nextjs With NextAuth App Router VS Pages Router
js With NextAuth –
App Router VS Pages Router
freecodecamp.org/news/how-to-authenticate-users-with-nextauth-in-nextjs-app-and-pages-router
This tutorial will show you an easy and secure way to handle authentication in your apps. It
involves allowing trusted organizations (such as Google, Facebook, Github, and so on) to
authenticate your users while you focus on building important features in your app.
You will learn how to do this using Nextjs and NextAuth step by step.
You should find this article helpful if you're new to NextAuth. But if you already know how to
set up authentication with the Nextjs Page router and you're still trying to transition to the
Nextjs App router, then this is for you, too.
Here's a video I made that you can use to supplement what you learn here.
Prerequisites
1/29
You need to know the basics of Next.js and Prisma to benefit from this tutorial.
What is NextAuth?
NextAuth is an open-source authentication solution for Nextjs applications. It is gradually
being developed to provide solutions for every framework or library on the web. You can find
more details on the Authjs website.
You can think about NextAuth as an intermediary between your application and established
authentication systems. So instead of re-inventing the wheel, you can add this solution to
yours and keep building your application.
The cool thing about this tool is that you do not have to pay money to use it. Just pay
attention. :)
This tutorial will cover three of them: Google, GitHub, and Email.
So you'll need to access credentials for each of these providers and bootstrap a boilerplate
for Nextjs.
You can follow the steps below to get your Google Client ID and Client Secret.
2/29
Click on the menu icon by the top-left corner. Select API & Services and then choose
Credentials.
3/29
The page that follows is the form below:
Enter the name of your application and click Create. You will be redirected to the
Credentials page:
4/29
Click on the Create Credentials button and select the OAuth client ID option:
You have to configure your consent screen to create an OAuth client ID. Click on the
Configure Consent Screen button to do this:
5/29
Set the User Type to external and click Create:
6/29
Scroll to the bottom of the page and click Save and Continue.
The Scopes page now appears. Click Save and Continue. Then click Save and Continue
on the Tests Users page. You will be directed to the Summary page. Review your information
and click Back To Dashboard.
Now try to Create an OAuth client ID, and you should come to the following page:
7/29
Select the Web application option because that is what suits this tutorial
8/29
Click Create, and that will produce the Client ID and Client Secret like in the image
below:
Copy them or download the JSON file. You will need them soon.
9/29
The directions below will guide you to get your GitHub Client ID and Client Secret.
Login to your Github dashboard and click on the profile image at the top-right-hand corner.
In the page that follows, scroll down and select Developer Settings:
10/29
Click on OAuth Apps on the next page and choose to Register a New Application:
Fill out the form that you see on the page that follows:
11/29
Click on the Register Application button. A new page appears with the GITHUB_ID and
GITHUB_SECRET:
12/29
You can use any email vendor that provides SMTP services. This tutorial will use Gmail. You
can check out Mailgun, SendGrid, and others.
EMAIL_SERVER_HOST
EMAIL_SERVER_PORT
EMAIL_SERVER_USER
EMAIL_SERVER_PASSWORD
EMAIL_FROM
Log in to your Gmail account. Click on your profile image. It will drop down a menu.
Click on the Manage your Google Account button. You will be redirected to the page below:
13/29
Click on Security on the side menu. The following page will appear.
You will need to set up 2-Step Verification for this process to work. If you have yours done
already, click on it.
It will require your password, after which you will be redirected to the following page:
14/29
Scroll to the bottom and click on App passwords, as in the image above. The page that
shows will look like the image below:
Select an App and a device from the drop-down and click on the Generate button:
15/29
That will now generate a 16-digit text like this:
EMAIL_SERVER_HOST="smtp.gmail.com"
EMAIL_SERVER_PORT=465
EMAIL_SERVER_USER="". An example is “[email protected]”
EMAIL_SERVER_PASSWORD=""
16/29
EMAIL_FROM="". This email doesn’t have to be in existence.
You will create 2 Next.js boilerplates. One will use the page router, while the other will work
with the app router.
Page Router
npx create-next-app@latest
You will now have a project created with a pages directory like this:
17/29
App Router
npx create-next-app@latest
18/29
What is your project named? app_router_tutorial
Would you like to use TypeScript with this project? No
Would you like to use ESLint with this project? No
Would you like to use Tailwind CSS with this project? No
Would you like to use `src/` directory with this project? No
Use App Router (recommended)? Yes
Would you like to customize the default import alias? No
You will now have a project created with an app directory like this:
19/29
Everything is now set. You will now begin creating the authentication application.
How to Setup Prisma and NextAuth on the Page and App Router
This section will focus on processes that didn’t change with the release of Next.js 13. So
everything you will do in this section will be applied to both the page_router_tutorial and
app_router_tutorial projects.
20/29
You will do the following:
Install Dependencies
Create and migrate a database model
Add the credentials that you got in the previous segment to a file
The command below will install all the modules for the project:
It adds NextAuth, Prisma, Node Mailer, Fauna DB, and Prisma Adapter modules to the
project.
A database model defines how tables in the database communicate with each other. You will
create that in this part.
It creates a Prisma directory with a file inside called schema.prisma and a .env file in the
project’s root folder. The file will contain your model definition.
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
This tutorial will use the Postgres database as indicated in the code above. The .env file has
the DATABASE_URL that you need to replace with your own. Do not forget to do that to avoid
bugs later in the project.
21/29
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
oauth_token_secret String?
oauth_token String?
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete:
Cascade)
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
This defines four (4) tables: Account, User, Session, and VerificationToken. The model
shows that a user can have multiple accounts, and each account may be logged into many
places (sessions). The VerificationToken table is for verifying a user.
22/29
It is now time to create the tables in the database. Run the command below to make it
happen:
You can then check your database dashboard for the new tables.
You can manage your database from the browser using Prisma by executing the command
below:
The .env file will hold all the details of your providers and other items. These are the
credentials:
EMAIL_SERVER_USER
EMAIL_SERVER_PASSWORD
EMAIL_SERVER_HOST
EMAIL_SERVER_PORT
EMAIL_FROM
GITHUB_ID
GITHUB_SECRET
GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET
Aside from these that you already know, you need two more:
NEXTAUTH_SECRET
NEXTAUTH_URL
NEXTAUTH_SECRET can be anything you choose. However, you want something hard to
predict. Run the following command to get a very random string:
NEXTAUTH_URL is the base URL of your project. Since the project is still in development, the
NEXTAUTH_URL is http://localhost:3000.
23/29
DATABASE_URL="postgres://postgres:[email protected].
co:5432/postgres"
EMAIL_SERVER_HOST="smtp.gmail.com"
EMAIL_SERVER_PORT=465
EMAIL_SERVER_USER="[email protected]"
EMAIL_SERVER_PASSWORD="cnbpfzfrjnxvfgcv"
EMAIL_FROM="[email protected]"
GOOGLE_CLIENT_ID="196506336558-
lh9qhavsi224v00n7q6ggn1f6cur9epr.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="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"
GITHUB_ID="11a8144f2897384cfedf"
GITHUB_SECRET="6599042d48110bbda9fcd1ec6ede61f4320adc9d"
NEXTAUTH_SECRET="YegyDIZNJPqxOkGS4K0F/o9l3SjCxCUR4Q/45rGyOtA="
NEXTAUTH_URL="http://localhost:3000"
That concludes what is the same process for both projects. The following section will now
focus on the page_router_tutorial directory.
Make a folder called auth in the pages/api/ directory. Create a file in the pages/api/auth
directory and name it [...nextauth].js.
Instantiate the PrismaClient and export NextAuth as default. Use the code below:
This code will automatically create and handle the API routes for authentication.
24/29
Next, export a NextAuth function and add the adapter to it like this:
The providers key is an array of functions, where each one is the provider that you want to
add to your project. You are to add three of them, so your code should look like this:
That is all you need to create an authentication application using Next.js and NextAuth.
25/29
Voilà...Surprise! Surprise! Surprise!
You didn’t create any UI, so where did this UI come from?
Now that is the beauty of NextAuth. It creates endpoints and an accompanying UI for each
provider. You don’t have to sweat it, but you can use your UI.
The following videos demonstrate how the authentication app you built works:
Google Provider
GitHub Provider
Email Provider - Magic Link
Signout
Click on the Signout button that shows when the page is done loading:
In this section, you learned how to build a Next.js authentication application using the pages
routing pattern. You can find the code for this segment on GitHub:
https://github.com/EBEREGIT/nextjs-nextauth-tut/tree/main/page_router_tutorial.
26/29
But Next.js version 13 was released a while ago and has gotten stable. It comes with the app
routing pattern, and Next.js developers are advised to embrace it since the pages router may
become deprecated soon.
You'll see how to set up Next.js authentication using the app routing pattern in the next
segment.
Do not forget to set up Prisma and NextAuth as you did in the previous section for the
page_router_tutorial project.
Create a file in the [...nextauth] folder called route.js. This is in line with Nextjs 13 Route
Handlers directive.
In the file, import PrismaAdapter, PrismaClient, NextAuth, and the providers like this:
Create a handler function to run the NextAuth configurations with the code below:
Pass the providers and adapter as one argument into the NextAuth method. Type the
following code:
27/29
const handler = NextAuth({
providers: [
GitHubProvider({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
],
adapter: PrismaAdapter(prisma),
});
That is all you need for the authentication to work with the app router.
You will have the same interface as in the previous section if you navigate to
http://localhost:3000/api/auth/signin.
Conclusion
Implementing authentication can be difficult. But with these helpful tools, it can only get
better. This tutorial aimed to teach how to implement authentication using NextAuth both in
the Next.js pages and app router.
28/29
You saw how to set up Prisma and NextAuth in any Next.js project. You also learned the
difference between Next.js pages and app routing patterns and how to implement the
NextAuth authentication logic.
This tutorial just scratched the surface, but it gives you an angle to start building personal
projects. Please check out the documentation below to keep learning.
React
Next
faunadb
NextAuth
Prisma
Postgres
😊
All the code for this tutorial is one GitHub: https://github.com/EBEREGIT/nextjs-nextauth-tut.
Please leave a star .
If you read this far, thank the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than
40,000 people get jobs as developers. Get started
29/29