Example of token based authentication with Node.js, Express.js, MongoDB and Mongoose.
Create a new user.
- Method:
POST
- URL:
/api/users
- Body:
{
"username": "art",
"password": "secret"
}
Authenticate user.
- Method:
POST
- URL:
/api/users/authenticate
- Body:
{
"username": "art",
"password": "secret"
}
Get items as an authenticated user.
- Method:
GET
- URL:
/api/items?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlbWEiLCJpYXQiOjE0NTEzMTMxOTgsImV4cCI6MTQ1MTMxNjc5OH0.TOi73nhmqGYU_Ajo-ufKcPk5TMmycyNSW3jDghPAHLc
Example of a token string: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlbWEiLCJpYXQiOjE0NTEzMTMxOTgsImV4cCI6MTQ1MTMxNjc5OH0.TOi73nhmqGYU_Ajo-ufKcPk5TMmycyNSW3jDghPAHLc
npm install
npm start