NodeJS RESTful API User Authentication with AngularJS using PHP MYSQL
In this Post We Will Explain About is NodeJS RESTful API User Authentication with AngularJS using PHP MYSQL With Example and Demo.
Welcome on Pakainfo.com – Examples ,The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to User login and registration using nodejs and mysql with example
In this post we will show you Best way to implement Token-Based Authentication With AngularJS & NodeJS, hear for How to RESTful API User Authentication with Node.js and AngularJS with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.
Step1:Create Table and Folder structure
CREATE TABLE `emploers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `cr_date` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
Now we will simple have to setup folders structure in following simple way :
├── appctrl │ └── authenticate-controller.js │ └── register-controller.js ├── node_modules ├── config.js ├── index.js └── package.json
In Node.js add or include package.json file is simple used to install here all the dependencies.
{ "name": "login", "version": "1.0.0", "description": "login authentication", "main": "index.js", "dependencies": { "body-parser": "^1.17.1", "express": "^4.14.1", "jsonwebtoken": "^7.3.0", "mysql": "^2.13.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
Step 2: here PHP Config.js to settings database connectivity
config.js
var mysql = require('mysql'); var live_connect = mysql.createConnection({ host : 'Pakainfo.com', user : 'livedata', password : 'live@#$%5558', database : 'Datacode' }); live_connect.connect(function(err){ if(!err) { console.log("simple Database is connected"); } else { console.log("Error while not connecting with database"); } }); module.exports = live_connect;
Step 3: index.js
Now here start with simple index.js file that is as well as entry point of the web- application.
index.js
var express=require("express"); var bodyParser=require('body-parser'); var app = express(); var authenticateController=require('./appctrl/authenticate-controller'); var registerController=require('./appctrl/register-controller'); app.use(bodyParser.urlencoded({extended:true})); app.use(bodyParser.json()); app.post('/api/register',registerController.register); app.post('/api/authenticate',authenticateController.authenticate); app.listen(8012);
Step 4: Create Register Controller
controller/register-controller.js
var live_connect = require('./../config'); module.exports.register=function(req,res){ var today = new Date(); var emploers={ "name":req.body.name, "email":req.body.email, "password":req.body.password, "cr_date":today, "updated_at":today } live_connect.query('INSERT INTO emploers SET ?',emploers, function (error, results, fields) { if (error) { res.json({ status:false, message:'there are some error with query' }) }else{ res.json({ status:true, data:results, message:'user registered sucessfully' }) } }); }
Step 5: Simple Create AngularJS Authenticate Controller
appctrl/authenticate-controller.js
var live_connect = require('./../config'); module.exports.authenticate=function(req,res){ var email=req.body.email; var password=req.body.password; live_connect.query('SELECT * FROM emploers WHERE email = ?',[email], function (error, results, fields) { if (error) { res.json({ status:false, message:'Here live24u are some error mistac with query' }) }else{ if(results.length >0){ if(password==results[0].password){ res.json({ status:true, message:'Pakainfo.com successfully smple authenticated congo!!' }) }else{ res.json({ status:false, message:"Pakainfo.com your Email Address with password does not match" }); } } else{ res.json({ status:false, message:"Pakainfo.com Email does not exits" }); } } }); }
I hope you have Got node.js – What does body-parser do with express in nodejs And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.