This project is a sample implementation of an authentication system that uses JSON Web Token (JWT). It is a REST API backend using nodejs, express and mongoDB as a database. The access token stored in the cookie.
- Basic Authentication (Register/Login with hashed password).
- JWT Tokens.
- get user,delete user and update user information.
- Included CORS.
/
│
├──── config/
│ └── database.js
├──── middleware/
│ └── Auth.js
├──── models/
│ └── User.js
├─── node_modules/
│
├── routes/
│ ├── auth.js
│ └── users.js
├── .env * .env file
├── .gitignore * git ignore file
├── index.js * Entry point of our Node's app
├── package.json * Defines our JavaScript dependencies
├── package-lock.json * Defines our exact JavaScript dependencies tree
└── README.md POST /api/auth/register
{
username,
email,
password
}
Description: creates a new user.th Password is stored in hashed format
POST /api/auth/login
{
email,
password
}
Description: logs in to the server. jwt token will be stored in the cookie
so that user still have access to the token in a secure way even after refreshing the page
GET /api/auth/logout
Description: delete jwt token from the cookie
- you can find out the rest of the APIs while using the project
Make sure you have the latest stable version of Node.js installed
- Clone this repository
git clone https://github.com/keynavas/auth-nodejs-jwt.gitor download repository as a ZIP file and uncompress it to your desired directory
- Navigate into the folder
cd auth-nodejs-jwt- Install NPM dependencies
npm installcreate .env file that contains:
MONGO_URI=mongoDB_URI
JWT_SECRET=jwt secret key
PORT=5000node index.jsOr use nodemon for live-reload
npm start