NextAuth the easiest authentication for Next.js

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Awesome!
I'm using it in a side project that I'm launching soon. I needed a specific provider and NextAuth is just great for that.
Glad you enjoyed it Victoria!
Interesting, thanks for writing this! 😊
I'd be curious how do you create protected pages and profile pages. I need to have a look and find out how to do it!
Thanks Catalin!
The protected pages are actually super simple with NextAuth. See this demo on their website:
https://next-auth-example.vercel.app/
Amazing! Thank you, Chris Bongers!
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Adding authentication to any application can be a bit of a mission. However, with NextAuth, it's a breeze for Next.js applications.
In this article, we'll explore a social login, using Twitter as our authentication layer.
We'll build this whole app from scratch so you can follow along.
Before adding it, let's quickly scaffold a new Next.js app.
npx create-next-app
Give the project a cool name on the prompt, and that's it. We have a basic Next.js app now.
To run your app enter the folder and execute the following command:
npm run dev
To add NextAuth to our project, we need to install the following package:
npm i next-auth@beta
The first step is to add the server-side layer. This comes with the callback for the providers and more.
To add this, we need to create an auth folder inside our pages/api folder.
Inside this auth folder create a file called [...nextauth].js.
Inside of it, place the following code.
import NextAuth from 'next-auth'
export default NextAuth({
providers: [
// We'll come back here
]
})
The next part we need is a session provider. This is already given to us by NextAuth. We just need to enable it in our pages/_app.js file.
import { SessionProvider } from 'next-auth/react';
function MyApp({ Component, pageProps: { session, ...pageProps } }) {
return (
<SessionProvider session={session}>
<Component {...pageProps} />
</SessionProvider>
);
}
export default MyApp;
This will make sure our complete app is wrapped in a session provider to access the session throughout our app.
So far, we don't have any providers set up yet, so our app can't authenticate with anything.
To change this, we must first register a new app in the Twitter Developer console.
Create a new app here.

Add this point go back to your application, and create a .env file in the root of your project.
Inside this file, add the following two details from your Twitter app.
TWITTER_CLIENT_ID=xxx
TWITTER_CLIENT_SECRET=xxx
Then click the authentication settings.

On this screen, fill out the following options:

The last thing we need to do is edit our pages/api/auth/[...nextauth].js file and include the Provider:
import NextAuth from 'next-auth';
import TwitterProvider from 'next-auth/providers/twitter';
export default NextAuth({
providers: [
TwitterProvider({
clientId: process.env.TWITTER_CLIENT_ID,
clientSecret: process.env.TWITTER_CLIENT_SECRET,
}),
],
});
Now that we have everything in place, let's add the login button to our app and test everything out.
Change your index.js file to look like this:
import { useSession, signIn, signOut } from 'next-auth/react';
export default function Component() {
const { data: session } = useSession();
if (session) {
return (
<>
Signed in as {session.user.email} <br />
<button onClick={() => signOut()}>Sign out</button>
</>
);
}
return (
<>
Not signed in <br />
<button onClick={() => signIn()}>Sign in</button>
</>
);
}
And if we now run our app, the flow looks like this:

You can also find the complete code on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter