NextJS authentication in 5 minutes
Tags: nextjs, nextauth, auth0, authentication
Published: 2021-10-24 13:14:56

---
title: NextJS authentication in 5 minutes
published: true
description: Basic NextJS authentication with next-auth and Auth0 done in 5 minutes.
tags: NextJS, nextauth, Auth0, authentication
//cover_image: https://direct_url_to_image.jpg
---
Hi everyone, in this article, I will show you how to quickly set up authentication in NextJS with next-auth and Auth0.

This article is great if you are just starting with the project because we will be using a bootstrapped project from next-auth.

NextAuth.js is a complete open-source authentication solution for Next.js applications. It is designed from the ground up to support Next.js and Serverless.

Auth0 is easy to implement, adaptable authentication and authorization platform.

## Clone repository and install dependencies
First we need to clone example app from next-auth. You can find the whole guide from next-auth [here] (https://github.com/nextauthjs/next-auth-example/tree/ndom91/update-v4) or just follow my steps.
1.	Clone the repository with this command `git clone https://github.com/nextauthjs/next-auth-example.git`
2.	Change directory to the cloned repository `cd next-auth-example`
3.	Install dependencies `npm install`
4.	Run the app `npm run dev`

Now you can open the app in the browser at http://localhost:3000

![App start screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r59rfgnd5n5lsxml2wkx.png)

## Auth0
1. Sign up for an Auth0 account at https://auth0.com/signup
2. When you get to the Getting Started screen select Applications from the left sidebar and then again Applications in the sub-menu. 
3. Click on the blue button with text "Create Application", select Regular Web Applications, press Create.

![Application created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jucd90zd2f7m5owojj9u.png)

By default, you have enabled Google and username-password authentications, you can check that at the Connections tab. 

For now, we will use that, but if you later want to enable more options, you can select Authentication->Social from the left sidebar and create new connections, after that enable it and Auth0 will take care of the rest.

## Configuration

After the app is created, go to the setting tab and you will see your Domain, Client ID and Client Secret which you need to provide to the next-auth.

In the settings tab, you need to provide a callback URL for Auth0. Scroll down until you see "Allowed Callback URLs" and paste this URL: http://localhost:3000/api/auth/callback/auth0

After you did this scroll to the bottom and press the "Save Changes" button.

![Settings tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19l4jldabpaasg9u2nv3.png)
 
Go to the repository api->auth->[...nextauth].js file, you can delete everything inside the providers array except Auth0 provider. Also, you can delete everything under the providers array since we don't need it for this example.

```javascript
import NextAuth from "next-auth"
import Providers from "next-auth/providers"

// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
export default NextAuth({
  // https://next-auth.js.org/configuration/providers
  providers: [
    Providers.Auth0({
      clientId: process.env.AUTH0_ID,
      clientSecret: process.env.AUTH0_SECRET,
      domain: process.env.AUTH0_DOMAIN,
    }),
  ],
})
```
Now open .env.local.example file located in the root directory, delete everything except AUTH0 env variables. Then assign them values from your Auth0 settings tab. 

![Env file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4gmx7c4e1c8q2xocxmr.png)
 

NOTE: Wrap strings in " " or else it won't work.

Lastly, rename the file from .env.local.example to .env

Now you can go to the http://localhost:3000 and press on Sign In button. You will be redirected to Auth0 generated page.

![Sign in with Auth0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unapold1o0mip1ei4mls.png)

Press on Sign in with Auth0 and you will be redirected to a new page with options to sign in, default is email-password and Google. Press on Google and Sign in with your account. After that, you will see you are signed in with your account on the Home page. Now you can access the protected route and if you log out you won't be able to see its content.

![Logged in user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cmsbvyudrnw8r8f8jxj.png)
 
This is it! Quick authentication in NextJS with next-auth and Auth0. If you have any questions, feel free to ask!