This project has been created to learn how to implement JSON Web Token (JWT) and cookie-based authentication for only an administrator (admin) using Next.js. The project includes a homepage that requires only users identified as administrators to log in. Users can log in using JWT tokens and cookies.
To enable JWT and cookie-based authentication in this project, we use the following environment variables. You should set these environment variables in the .env
file:
-
NEXT_PUBLIC_ADMIN_SECRET
: A variable that contains the password used for admin login. This value is compared to create a JWT token and stored as a cookie. -
NEXT_PUBLIC_JWT_SECRET_KEY
: A secret key used to sign and verify JWT tokens. This key ensures the security of JWT tokens. -
To start the project, run the following commands;
npm i
andnpm run dev
: -
Visit
http://localhost:3000
in your web browser. The homepage will appear. -
Click the "Log In" button on the homepage to log in as a user identified as an administrator. You should log in using the
NEXT_PUBLIC_ADMIN_SECRET
value you've set earlier. -
After a successful login, you will be redirected to the administrator page.
The purpose of this project is to learn the fundamental concepts of implementing JWT and cookie-based authentication for only administrator users using Next.js. By examining the project, you can learn how to grant access to specific areas that require a certain level of authentication.
If you wish to customize the project and add more features, you can refer to the official Next.js documentation and other resources related to JWT.