Skip to content

Basic template you can use in your Node Backends for JWT authentication. Uses Node.js, Express.js and MongoDB.

License

Notifications You must be signed in to change notification settings

codetit4n/node-jwt-auth-template

Repository files navigation

JWT authentication template for Node Backend

Basic template you can use in your projects for JWT authentication. It is built using Node.js, Express.js and MongoDB.

Routes:

/api/user/register - POST

Creates a new user. It takes name, email and password in the request body and stores the name, email and hashed password in the Mongo database.

/api/user/login - POST

Logs in the registered user and sends a new signed JWT. This JWT can be stored in local storage, etc.

/api/protected - GET

Simple protected route, just for testing the JWT. You will need a valid JWT to access this route. Put the JWT in the header like: Authorization: 'Bearer [JWT]'

How to use this template:

  1. Clone the repo using:
git clone https://github.com/codeTIT4N/node-jwt-auth-template.git
  1. Rename .env.example to .env

  2. Update the .env file with project secrets like:

  3. Go inside the project and run the following commands:

npm install
npm start
  1. Edit the routes/protected.js file for the protected routes according to your project's needs. One example is provided in this file.

  2. Edit the controllers/protected.js file for the controllers that will be executed after JWT validation. One example is provided in this file.

Releases

No releases published

Packages

No packages published