Node.js and MongoDB API for authentication with JSON Web Tokens
$ npm install$ npm run server # Runs on http://localhost:5000$ npm startTo connect to MongoDB you need to create a config.env file in the /config directory of the project. The file should have a format like
this:
MONGO_URI=YOUR_CONNECTION_STRING
NODE_ENV=development
SECRET_KEY=MYSECRETKEY
-
POST /api/users/: Registers a new user -
POST /api/auth/: Authenticates user and generates token -
GET /api/auth/current: Gets current user (requires json web token)
Send POST request to /api/users/ with a JSON body like this:
{
"email": "email@email.com",
"username": "yourusername",
"password": "123456"
}Send POST request to /api/auth with a JSON body like this:
{
"email": "email@email.com",
"password": "123456"
}For protected routes you need to add your token to the Authorization HTTP header.
For example:
Authorization: Bearer YOUR_TOKEN
1.0.0
Andres Aguilar Moya