NODE Auth
It is a project to help you to create an api using nodejs, with this kit, you will avoid starting from the very beginning using authentication and MongoDB as a database.
How to use
To use it, you will need to have a mongodb server and change it's config on config.js
into src
folder.
If you'll run it into a Docker using the project's docker-compose file, you don't need to take care with it (mongo server).
config.js
module.exports = {
database: 'mongodb://localhost:27017/node-auth'
};
OR For docker users:
module.exports = {
database: 'mongodb://database/node-auth'
};
Running api
1 - git clone https://github.com/abdalla/node-auth.git
2 - npm install
3 - npm start
To use into Docker
1 - git clone https://github.com/abdalla/node-auth.git
2 - npm install
3 - docker-compose up
Running tests
To run the tests you don't need to have mongodb installed.
Running tests to make sure everything is working good.
It is pretty simple...
npm test
or to get coverage statistics
npm run test-cover
Routes
Setup
Used to create the first user (admin)
Verbose: POST
http://localhost:3000/api/setup
Response:
{
"success": true,
"user": { user information }
}
Authentication
Used to authenticate the user and get the token used to comunicate with the other routes.
Verbose: POST
http://localhost:3000/api/auth
Header:
Content-Type: application/json
Body:
{
"email": "[email protected]",
"password": "admin"
}
Response:
{
"success": true,
"message": "enjoy",
"token": "<token>"
}
Get all users
Used to get all users.
Verbose: GET
http://localhost:3000/api/users
Header:
x-access-token: "<token>"
Response:
{
"success": true,
"users": []
}
Get user by id
Used to get a specific user.
Verbose: GET
http://localhost:3000/api/user/{userId}
Header:
x-access-token: "<token>"
Response:
{
"success": true,
"user": { user information }
}
Create new user
Used to create a new user.
Verbose: POST
http://localhost:3000/api/user
Header:
Content-Type: application/json
x-access-token: "<token>"
Body:
{
"user": {
"name": "user full name",
"userName": "user name",
"password": "password",
"email": "[email protected]",
"admin": false
}
}
Response:
{
"success": true,
"user": { user information }
}
Update an user
Used to update an user information (except password).
Verbose: PUT
http://localhost:3000/api/user
Header:
Content-Type: application/json
x-access-token: "<token>"
Body:
{
"user": {
"_id": REQUIRED,
"name": "user full name (optional)",
"userName": "user name (optional)",
"email": "[email protected] (optional)",
"admin": false(optional)
}
}
Response:
{
"success": true,
"user": { user information }
}
Update user password
Used to change the user password.
Verbose: PUT
http://localhost:3000/api/userpassword/{userId}
Header:
Content-Type: application/json
x-access-token: "<token>"
Body:
{
"currentPassword": "currentPassword",
"newPassword": "newPassword"
}
Response:
{
"success": true,
"user": { user information
}
}
Delete an user
Used to delete an user.
Verbose: DELETE
http://localhost:3000/api/user/<userId>
Header:
x-access-token: "<token>"
Response:
{
"success": true,
"user": { user information }
}
Core libraries
For App
Express - The most popular Node framework ( thinking to change to koa )
Mongoose - Interact with our MongoDB database
Morgan - Log requests to the console so we can see what is happening
body-parser - Get parameters from our POST requests
jsonwebtoken - Create and Verify our JSON Web Tokens
bluebird - Promise
For Test
Mocha - The testing framework
Chai - Gives you some useful tools for testing, such as expect/should functions.
mongodb-memory-server - Allows you to run Mongoose in-memory instead of connecting to a persistent database.
Factory-girl - For creating factories for your models.
Faker - For creating randomized data
Supertest - For performing requests in your tests
Istanbul - JS code coverage tool