JWTauthentication API bolilerplate
To run the demo web app offline, you need to install the following;
- Make sure you have installed Node.js. Versions 6.0.0 and higher should work. We recommend that you use the most-recent
"Active LTS"
version of Node.js. - Make sure you have installed
npm
(NB:npm
is distributed with Node.js- which means that when you download Node.js, you automatically get npm installed on your computer).
- To check if you have Node.js installed, run this command in your terminal:
node -v
- To confirm that you have npm installed you can run this command in your terminal:
npm -v
npm install
node index
(recommend install nodemon)- Open http://localhost:3000
This project is a JWT user authentication API bolier plate that can be used in a node project that requires . The application has this structure:
├── app
│ ├── controllers
│ │ └── user.js # Exports functions to use in routers
│ ├── logs # Contains all the logs
│ ├── models # Contains all models
│ │ └── User.js # User model
│ └── routes # contains all routes
│ ├── api # where /api/ routes are stored
│ │ ├── index.js # separates public from private routes with a middleware
│ │ ├── private.js # contains all private routes
│ │ └── public.js # contains all public routes
│ └── index.js # where / routes are stored
│
|
├── utils.js # useful functions used in the entire application
├── config.js # where all JWT, Winston, Mongoose,... config goes
├── index.js # entry point, where mongoose connects to mongodb
└── server.js # where the server starts and routes for the root path
- Writing tests
- Code review
- Other guidelines
- Repo owner or admin: Reuben Antz. Feel free to contact Antz if you wanna Fork the project to try something unique, and test.
nodemon
will start a development server with nodemon
POST /api/user/register
Parameters:
email
uniquefirstName
lastName
password
POST /api/user/login
Parameters:
email
password
Returns:
user
user objecttoken
jwt token
In order to access private routes you MUST pass the token
parameter that is given when logging in.
GET /api/user
- Returns the user from the database, useful for refreshing user data.
POST /api/user
- Updates user data
Parameters:
Any user field that you want to update, _id
, meta
and __v
will be ignored.
POST /api/user/checkpassword
- Returns true if the provided password is the same as the user's one
Parameters:
password
Current user password that you want to check.
DELETE /api/user
- Deletes the user
LANDING PAGE
- If success recieve:
{ "status": 200, "message": "Trust me, you made it! Best regards from Antz" }
Login
Post('http://localhost:3000/api/user/login', {
{"key":"email","value":"[email protected]\n","description":"","type":"text","enabled":true},{"key":"password","value":"12345678","description":"","type":"text","enabled":true}
- If success recieve:
{
"success": true,
"user": {
"_id": "5a93e06ad884f70b59362620",
"firstName": "ANtz",
"lastName": "Antz",
"email": "[email protected]\n",
"__v": 0,
"meta": {
"updated_at": "2018-02-26T10:24:42.106Z",
"created_at": "2018-02-26T10:24:42.106Z"
}
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTkzZTA2YWQ4ODRmNzBiNTkzNjI2MjAiLCJmaXJzdE5hbWUiOiJBTnR6IiwibGFzdE5hbWUiOiJBbnR6IiwiZW1haWwiOiJhbnR6QGdtYWlsLmNvbVxuIiwiX192IjowLCJtZXRhIjp7InVwZGF0ZWRfYXQiOiIyMDE4LTAyLTI2VDEwOjI0OjQyLjEwNloiLCJjcmVhdGVkX2F0IjoiMjAxOC0wMi0yNlQxMDoyNDo0Mi4xMDZaIn0sImlhdCI6MTUxOTY0MDY5OCwiZXhwIjoxNTIyMjMyNjk4fQ.LFjUBd9y0T4CWz3DKltTYam8e5HwJl2rj2_Q82E6qDY"
}
- If failed recieve:
{
"success": false,
"message": "Wrong email or password!"
}
Register
Post('localhost:3000/api/user/register', {
[{"key":"firstName","value":"Mike","description":"","type":"text","enabled":true},{"key":"lastName","value":"Alex","description":"","type":"text","enabled":true},{"key":"email","value":"[email protected]\n","description":"","type":"text","enabled":true},{"key":"password","value":"1234567d","description":"","type":"text","enabled":true}]
- If success recieve:
{
"success": true
}