fredrick esedo
2018-08-17 16:07:41 UTC
Am using the following code to authenticate users. when they logout of
dashboard., the session is not destroy as a result if i click back to the
dashboard i will still users login session data.
Here is the
app.js code
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
//var methodOverride = require('method-override');
var session = require('express-session');
var app = express();
var mysql = require('mysql');
var cookieParser = require('cookie-parser');
var bodyParser=require("body-parser");
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'nodejs'
});
connection.connect();
global.db = connection;
// all environments
app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
//app.set('views', __dirname + '/views');
//app.engine('html', require('ejs').renderFile);
//app.set('view engine', 'html');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
key: 'user_sid',
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { maxAge: 60000 }
}));
app.use(function(req, res, next){
res.locals.session = req.session;
next();
});
// development only
app.get('/', routes.index);//call for main index page
app.get('/signup', user.signup);//call for signup page
app.post('/signup', user.signup);//call for signup post
app.get('/login', routes.index);//call for login page
app.post('/login', user.login);//call for login post
app.get('/home/dashboard', user.dashboard);//call for dashboard page after
login
app.get('/home/logout', user.logout);//call for logout
app.get('/home/profile',user.profile);//to render users profile
//Middleware
app.listen(8080)
here is the user.js code
//---------------------------------------------signup page
call------------------------------------------------------
exports.signup = function(req, res){
message = '';
if(req.method == "POST"){
var post = req.body;
var name= post.user_name;
var pass= post.password;
var fname= post.first_name;
var lname= post.last_name;
var mob= post.mob_no;
var sql = "INSERT INTO
`users1`(`first_name`,`last_name`,`mob_no`,`user_name`, `password`) VALUES
('" + fname + "','" + lname + "','" + mob + "','" + name + "','" + pass +
"')";
var query = db.query(sql, function(err, result) {
message = "Succesfully! Your account has been created.";
res.render('signup.ejs',{message: message});
});
} else {
res.render('signup');
}
};
//-----------------------------------------------login page
call------------------------------------------------------
exports.login = function(req, res){
var message = '';
var sess = req.session;
if(req.method == "POST"){
var post = req.body;
var name= post.user_name;
var pass= post.password;
var sql="SELECT id, first_name, last_name, user_name FROM `users1`
WHERE `user_name`='"+name+"' and password = '"+pass+"'";
db.query(sql, function(err, results){
if(results.length){
req.session.userId = results[0].id;
req.session.user = results[0].id;
req.session.last_name =results[0].last_name;
console.log(results[0].id);
console.log(req.session.last_name);
//res.redirect('/home/dashboard',{data:req.session.last_name});
res.redirect('/home/dashboard');
}
else{
message = 'Wrong Credentials.';
res.render('index.ejs',{message: message});
}
});
} else {
res.render('index.ejs',{message: message});
}
};
//-----------------------------------------------dashboard page
functionality----------------------------------------------
exports.dashboard = function(req, res, next){
var user = req.session.user,
userId = req.session.userId;
console.log('ddd='+userId);
var seco= req.session.last_name;
console.log(seco);
if(userId == null){
res.redirect("/login");
return;
}
var sql="SELECT * FROM `users1` WHERE `id`='"+userId+"'";
db.query(sql, function(err, results){
res.render('dashboard.ejs', {user:user, sec1:seco, user1:userId});
});
};
//------------------------------------logout
functionality----------------------------------------------
exports.logout=function(req,res){
res.clearCookie('myCookie');
res.clearCookie('connect.sid');
req.session.destroy(function(err) {
//console.log(user);
res.redirect("/login");
})
};
//--------------------------------render user details after
login--------------------------------
exports.profile = function(req, res){
var userId = req.session.userId;
if(userId == null){
res.redirect("/login");
return;
}
var sql="SELECT * FROM `users1` WHERE `id`='"+userId+"'";
db.query(sql, function(err, result){
res.render('profile.ejs',{data:result});
});
};
//---------------------------------edit users details after
login----------------------------------
exports.editprofile=function(req,res){
var userId = req.session.userId;
if(userId == null){
res.redirect("/login");
return;
}
var sql="SELECT * FROM `users1` WHERE `id`='"+userId+"'";
db.query(sql, function(err, results){
res.render('edit_profile.ejs',{data:results});
});
};
Thanks
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/1f00a449-a625-414f-9e32-0d8e89f32604%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
dashboard., the session is not destroy as a result if i click back to the
dashboard i will still users login session data.
Here is the
app.js code
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
//var methodOverride = require('method-override');
var session = require('express-session');
var app = express();
var mysql = require('mysql');
var cookieParser = require('cookie-parser');
var bodyParser=require("body-parser");
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'nodejs'
});
connection.connect();
global.db = connection;
// all environments
app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
//app.set('views', __dirname + '/views');
//app.engine('html', require('ejs').renderFile);
//app.set('view engine', 'html');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
key: 'user_sid',
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { maxAge: 60000 }
}));
app.use(function(req, res, next){
res.locals.session = req.session;
next();
});
// development only
app.get('/', routes.index);//call for main index page
app.get('/signup', user.signup);//call for signup page
app.post('/signup', user.signup);//call for signup post
app.get('/login', routes.index);//call for login page
app.post('/login', user.login);//call for login post
app.get('/home/dashboard', user.dashboard);//call for dashboard page after
login
app.get('/home/logout', user.logout);//call for logout
app.get('/home/profile',user.profile);//to render users profile
//Middleware
app.listen(8080)
here is the user.js code
//---------------------------------------------signup page
call------------------------------------------------------
exports.signup = function(req, res){
message = '';
if(req.method == "POST"){
var post = req.body;
var name= post.user_name;
var pass= post.password;
var fname= post.first_name;
var lname= post.last_name;
var mob= post.mob_no;
var sql = "INSERT INTO
`users1`(`first_name`,`last_name`,`mob_no`,`user_name`, `password`) VALUES
('" + fname + "','" + lname + "','" + mob + "','" + name + "','" + pass +
"')";
var query = db.query(sql, function(err, result) {
message = "Succesfully! Your account has been created.";
res.render('signup.ejs',{message: message});
});
} else {
res.render('signup');
}
};
//-----------------------------------------------login page
call------------------------------------------------------
exports.login = function(req, res){
var message = '';
var sess = req.session;
if(req.method == "POST"){
var post = req.body;
var name= post.user_name;
var pass= post.password;
var sql="SELECT id, first_name, last_name, user_name FROM `users1`
WHERE `user_name`='"+name+"' and password = '"+pass+"'";
db.query(sql, function(err, results){
if(results.length){
req.session.userId = results[0].id;
req.session.user = results[0].id;
req.session.last_name =results[0].last_name;
console.log(results[0].id);
console.log(req.session.last_name);
//res.redirect('/home/dashboard',{data:req.session.last_name});
res.redirect('/home/dashboard');
}
else{
message = 'Wrong Credentials.';
res.render('index.ejs',{message: message});
}
});
} else {
res.render('index.ejs',{message: message});
}
};
//-----------------------------------------------dashboard page
functionality----------------------------------------------
exports.dashboard = function(req, res, next){
var user = req.session.user,
userId = req.session.userId;
console.log('ddd='+userId);
var seco= req.session.last_name;
console.log(seco);
if(userId == null){
res.redirect("/login");
return;
}
var sql="SELECT * FROM `users1` WHERE `id`='"+userId+"'";
db.query(sql, function(err, results){
res.render('dashboard.ejs', {user:user, sec1:seco, user1:userId});
});
};
//------------------------------------logout
functionality----------------------------------------------
exports.logout=function(req,res){
res.clearCookie('myCookie');
res.clearCookie('connect.sid');
req.session.destroy(function(err) {
//console.log(user);
res.redirect("/login");
})
};
//--------------------------------render user details after
login--------------------------------
exports.profile = function(req, res){
var userId = req.session.userId;
if(userId == null){
res.redirect("/login");
return;
}
var sql="SELECT * FROM `users1` WHERE `id`='"+userId+"'";
db.query(sql, function(err, result){
res.render('profile.ejs',{data:result});
});
};
//---------------------------------edit users details after
login----------------------------------
exports.editprofile=function(req,res){
var userId = req.session.userId;
if(userId == null){
res.redirect("/login");
return;
}
var sql="SELECT * FROM `users1` WHERE `id`='"+userId+"'";
db.query(sql, function(err, results){
res.render('edit_profile.ejs',{data:results});
});
};
Thanks
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/1f00a449-a625-414f-9e32-0d8e89f32604%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.