Every application needs a way to register new users and then authenticate them.
Node data provides an easier way to implement it.
Prerequisite–
- mongodb is installed and server is running on default port.
- Node 6.9.0 is installed.
- IDE like vscode is available to you.
- Rest client like postman,curl for testing.
Installation
- git clone https://github.com/riteshagarwal100/nodedataauth.git
- cd nodedataauth/Demo-Sample
- npm install
Code To Verify
- Check the below code in current-user-detail-service.ts present in root directory
- This code is managed by the developer, who decides how to parse the incoming user register request and then save it in users custom mongodb collection.
- Node data uses bcrypt-nodejs, so encrypt the password field using it.
[gist]74e85352469c183543609405098dbad0[/gist]
Testing
- npm start
- Post a JSON {
“name”:”username”,
“password”:”pwd”
} to http://localhost:9999/register - This will create a new user in the data base,with above credentials
- Post a JSON {
“username”:”username”,
“password”:”pwd”
} to http://localhost:9999/login - This will authenticate the user, and user will be logged in.
- This will create a session for this user
- Post a JSON {
“name”:”teach”,
“courses”:[“a”,”b”]
}to http://localhost:9999/data/teacher - This will create a teacher Object in data base
- Hit GET for http://localhost:9999/data/teacher
- This will return all the teachers present in the data base
Conclusion
It’s super easy to create authentication using node data.
https://github.com/ratneshsinghparihar/Node-Data
Or visit our main page