yxyx136
yxyx136

Reputation: 17

Nodejs Express: Sending a different page if logged in

I'm trying to create a website using expressjs. I want to send a different page to the user if he's logged in.

I'm able to send the raw html website, but linked files like index.css are not beeing loaded!

app.use(cookieParser());
app.use(expressSession({secret:'somesecrettokenhere', resave: false, saveUninitialized: true}));
app.use(bodyParser());

//app.use(express.static(path.join(__dirname, '../client/login')));
// disable layout
var mainPage  = express.static(path.join(__dirname, '../client/main'));
var loginPage = express.static(path.join(__dirname, '../client/login'));

app.get('/', function (req, res, next) {
    console.log('getting /');
    if (req.session.userName) {
        //app.use(express.static(path.join(__dirname, '../client/main')));
        console.log("logged in");
        mainPage(req, res, next);
    }else {
        //app.use(express.static(path.join(__dirname, '../client/login')));
        console.log("not logged in");
        loginPage(req, res, next);
    }
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

Thanks in advance!

Upvotes: 1

Views: 2694

Answers (2)

vinitmuchhala
vinitmuchhala

Reputation: 41

Yes, you can. You can do response.render(pageName); to render the page where you want to send the user.

app.use(cookieParser());
app.use(expressSession({secret:'somesecrettokenhere', resave: false, saveUninitialized: true}));
app.use(bodyParser());

//app.use(express.static(path.join(__dirname, '../client/login')));
// disable layout
// view engine setup
app.set('views', path.join(__dirname, '../client'));
app.set('view engine', 'jade');
app.get('/', function (req, res, next) {
   console.log('getting /');
   if (req.session.userName) {
       //app.use(express.static(path.join(__dirname, '../client/main')));
       console.log("logged in");
       res.render("mainPage");
    }else {
       //app.use(express.static(path.join(__dirname, '../client/login')));
       console.log("not logged in");
       res.render("loginPage");
   }
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

I used jade templating engine, you can use any other

Upvotes: 1

rdegges
rdegges

Reputation: 33864

It looks like the code you have is just checking a session for a user name -- I'd highly recommend AGAINST doing this, as it's not a secure way to do authentication.

If you want a secure way to do auth you should look into using a tool like either passportjs or stormpath. Stormpath does a bunch of stuff (api auth, social login, etc) in addition to the normal auth stuff, while passport is a bit more bare-bones.

Here's an example using Stormpath to do what you want:

app.get('/', function(req, res) {
  if (req.user) {
    res.render('template');
  } else {
    res.render('anonymousTemplate');
  }
});

Full disclosure: I work @ Stormpath and am the author of that library -- but it is awesome all the same.

Upvotes: 0

Related Questions