A simple Next.js application to demonstrate an authentication flow using JWT (JSON Web Tokens).
- Clone the repository:
git clone [repository-url]
cd next-app-auth
- Install the dependencies:
npm install
- Start the development server:
npm run dev
Your application should now be running on http://localhost:3000.
-
pages/
- This directory contains all the pages of your Next.js application.api/
- API routes are located here. We've got thelogin
API route that checks the credentials and returns a JWT.dashboard.js
- Represents the dashboard page which is shown to the user once they're authenticated.login.js
- Contains the login form.index.js
(orpage.js
) - Home page that redirects users based on their authentication status.
-
package.json
- Lists the project dependencies and contains scripts for development, build, and test.
- The user navigates to the login page and enters their credentials.
- Upon clicking the login button, a request is sent to the
/api/login
route. - This route checks the credentials and, if valid, returns a JWT.
- This token is then saved to local storage and used to determine if the user is authenticated throughout the app.
- If the user navigates to the dashboard, their authentication status is checked using the presence of the JWT in local storage.
- A logout button on the dashboard allows the user to remove their token from local storage and be redirected to the login page.
next
- The React framework for building the application.jsonwebtoken
- Used for generating and verifying JWTs.react
,react-dom
- React library.- Dev dependencies include various tools and libraries to help with styling, testing, and transpilation.
dev
: Runs the development server.build
: Builds the production version of the app.start
: Starts the production server.lint
: Lints the codebase using Next.js lint.test
: Runs tests using Jest.
For more information, feel free to navigate through the codebase or reach out to the project maintainer.
That's a basic README that covers the main points of your project. You can expand upon it further, adding sections as you deem necessary (e.g., code conventions, how to contribute, future enhancements, etc.).