Skip to content

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

License

Notifications You must be signed in to change notification settings

codetit4n/node-jwt-auth-template-ts

Repository files navigation

JWT authentication template for Node Backend - TypeScript edition

Basic template you can use in your projects for JWT authentication. It is built using Node.js, Express.js and MongoDB. It has amazing Type Security which is possible because of TypeScript.

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-ts
  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 run build
npm start
  1. Edit the src/routes/protected.ts file for the protected routes according to your project's needs. One example is provided in this file.

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

  3. To compile TypeScript code to JavaScript and see the changes in real time, in a different terminal instance, run:

tsc -w

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published