correction bug

This commit is contained in:
adri 2019-01-07 22:50:35 +01:00
parent d1b502c110
commit 51558b193f
2 changed files with 66 additions and 69 deletions

View File

@ -1,66 +1,65 @@
//http://www.expertphp.in/article/user-login-and-registration-using-nodejs-and-mysql-with-example //http://www.expertphp.in/article/user-login-and-registration-using-nodejs-and-mysql-with-example
var connection = require('./../bdd'); var connection = require('./../bdd');
var passwordProtection = require('./securepw'); var passwordProtection = require('./securepw');
const util = require('util'); const util = require('util');
module.exports=function(req,res,next){ module.exports = function(req, res, next) {
var name=req.body.username; var name = req.body.username;
var password=passwordProtection(req.body.password); var password = passwordProtection(req.body.password);
connection.query('SELECT * FROM user WHERE userName = ?',[name], function (error, results, fields) { connection.query('SELECT * FROM user WHERE userName = ?', [name], function(error, results, fields) {
if (error) { if (error) {
console.log("ERROR SQL LOGIN-CONTROLLER : "+console.error); console.log("ERROR SQL LOGIN-CONTROLLER : " + console.error);
req.session.authStatus=false; req.session.authStatus = false;
req.session.authMessage='SQL0'+error; req.session.authMessage = 'SQL0' + error;
next(); next();
}else{ } else {
if(results.length >0){ if (results.length > 0) {
if(password==results[0].pw){ if (password == results[0].pw) {
req.session.userName = name; req.session.userName = name;
req.session.authStatus=true; req.session.authStatus = true;
req.session.authMessage='OK'; req.session.authMessage = 'OK';
console.log(name + " SUCESSFULL LOGED");
next(); next();
}else{ } else {
console.log(name + " BAD PASSWORD");
req.session.authStatus=false, req.session.authStatus = false,
req.session.authMessage="BAD PASSWORD"; req.session.authMessage = "BAD PASSWORD";
next(); next();
} }
} } else {
else{ if (req.body.password != "") {
if(req.body.password != "") var users = {
{ "userName": req.body.username,
var users={ "pw": passwordProtection(req.body.password)
"userName":req.body.username, }
"pw": passwordProtection(req.body.password) connection.query('INSERT INTO user SET ?', users, function(error, results, fields) {
} if (error) {
connection.query('INSERT INTO user SET ?',users, function (error, results, fields) { //res.local.stat = 0;
if (error) { res.locals.message = '0';
//res.local.stat = 0; console.log("ERROR SQL LOGIN-CONTROLLER : " + error);
res.locals.message='0'; req.session.authStatus = false;
console.log("ERROR SQL LOGIN-CONTROLLER : "+console.error); req.session.authMessage = 'SQL INSERT ERROR';
req.session.authStatus=false; next();
req.session.authMessage='SQL INSERT ERROR'; } else {
next(); req.session.userName = name;
}else{ req.session.authStatus = true;
req.session.userName = name; req.session.authMessage = 'SQL INSERT OK';
req.session.authStatus=true; console.log(name + "SUCESSFULL REGISTER"+req.session.authMessage);
req.session.authMessage='SQL INSERT OK'; next();
next(); }
} });
}); } else {
} req.session.userName = name;
else { req.session.authStatus = true;
req.session.userName = name; req.session.authMessage = 'AUTH WITHOUT REG OK';
req.session.authStatus=true; console.log(name + "SUCESSFULL LOGED WITHOUTH PW");
req.session.authMessage='AUTH WITHOUT REG OK'; next();
} }
next(); }
} }
} });
}); }
}

View File

@ -28,9 +28,7 @@ app.get('/', function(req, res){
res.redirect('/login'); res.redirect('/login');
}).get('/login', function(req, res){ }).get('/login', function(req, res){
res.render('login'); res.render('login');
}).post('/login', function(req,res,next){ }).post('/login', loginController, function(req, res){
next();
}, loginController, function(req, res){
res.send('<!DOCTYPE html><html><body>'+req.session.authMessage+'<br><a href="/">HOME</a></body></html>'); res.send('<!DOCTYPE html><html><body>'+req.session.authMessage+'<br><a href="/">HOME</a></body></html>');
}); });