In this lesson, we are going to walk you through the process of creating a user authentication system using Next.js and Auth.js, which is formerly know as NextAuth.js. In a lot of tutorials, you'll see both names used interchangeably, even in the official documentation, please know that they are referring to the same thing. For this course, we'll stick to Auth.js for consistency.
Auth.js is a user authentication library that simplifies secure integration with databases, email services, OAuth providers, and everything that is required to create a seamless and robust authentication experience for their applications.
Installing Auth.js
To get started, navigate to the root directory of our project and run the following command to install the Auth.js package.
1npm install next-auth@beta
Auth.js requires you to generate a secret key, which will be used to encrypt tokens and email verification hashes. It is very important to keep this secret key secret. Exposing this key will lead to serious security risks.
The key can be generated with the following command:
1npx auth secret --copy
1Need to install the following packages:
2auth@1.2.3
3Ok to proceed? (y)
4š Secret generated. It has been copied to your clipboard, paste it to your .env/.env.local file to continue.
5
6AUTH_SECRET="<secret_key>"
This command will generate a key and copy it to your clipboard. You can paste it into the .env
file under the project root directory. Manually create the .env
file if it doesn't exist.