0

I have 2 main pages. One page is

  • mywebsite.com/login

  • mywebsite.com

/login page shows login and password page. When the user successfully gets authenticated, the mywebsite.com page opens.

mywebsite.com/login goes to login.js file on server

mywebsite.com/ goes to index.js file on the server

I want to maintain a session across both page. If the user isn't loggedin, I want the mywebsite.com/ to redirect to /login.

I have read about req.session but I don't know how to know about req.session value of login.js inside index.js.

app.js

var login = require('./routes/login');
var routes = require('./routes/index');

var app = express();

app.use('/login', login);
app.use('/', routes);


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

login.js

router.get('/', function(req, res, next) {
  res.render('login', { title: 'Login' });
});

router.post('/isEmployeeAdmin', function(req, res, next) {
  console.log("req is: ", req.body);
  employeeController.isEmployeeAdmin(req.body, function (response, data) {
    response.data = data;
    if (!response.success) {
      console.error("error is: ", response.message);
      res.status(200).send(response);
      return;
    }

    res.status(200).send(response);
    console.log("success in finding if employee is admin ", response.data);
  });
});

index.js

router.get('/', function(req, res, next) {

    if(req.session.loggedIn)
          res.render('index', { title: 'WinGoku' });
    else
        res.status(404).send(); // or redirect to login.ejs file
});

What am I doing wrong? How can I do it?

user2498079
  • 2,872
  • 8
  • 32
  • 60
  • Here's some basic info on using middleware for sessions - http://www.codexpedia.com/node-js/a-very-basic-session-auth-in-node-js-with-express-js/ – metame Feb 16 '17 at 19:06
  • Possible duplicate of [how to implement login auth in node.js](http://stackoverflow.com/questions/7990890/how-to-implement-login-auth-in-node-js) – metame Feb 16 '17 at 19:09

1 Answers1

0

I suggest you use the express-session npm Module to maintain sessions. https://www.npmjs.com/package/express-session

You need to include it in your app.js

var session = require('express-session');

Give it a secret key

app.use(session({secret: 'anystringhere'}));

And change this code a bit

if (!response.success) {
      console.error("error is: ", response.message);
      res.status(200).send(response);
      return;
    }
else{
   req.session.loggedIn = true;
}

This should do the trick

Sharjeel Ahmed
  • 2,051
  • 2
  • 17
  • 25
  • Will the req.session.loggedIn value inside login.js be accessible in index.js as well? – user2498079 Feb 16 '17 at 19:12
  • Yes of course, it is a session variable, it will be accessible to all functions within the current session. – Sharjeel Ahmed Feb 16 '17 at 19:33
  • How do I redirect to index.ejs file? I am trying to do res.redirect('/').send(response); but it's not working. response object contains data that will be used by client side js file – user2498079 Feb 16 '17 at 19:33
  • Thats a completely different topic, and to pass data to client side JS file is very different. but a simple res.redirect("/") should work to redirect. – Sharjeel Ahmed Feb 16 '17 at 19:46