0

Using passport.js I don't need any client side javascript until the user is logged in.

On my server side, I have a router that handles some login logic. So when the user successfully logs in, I route the user to the /profile route.

This kind of confuses me, since express is handling the overall framework. The /profile route renders my profile template res.render('profile', { user : req.user }); and nicely passes in the server model for the user into the template, cool stuff!

But this is what gets me, if my app is now on /profile a path that doesn't exist in my directory and I define my directory here app.use('/', express.static(path.join(__dirname, 'public'))); this results in bad architecture.

Because at times I get 404 errors on my js css. I have to route like this /routes#chat on my client side backbone router.

My Question

So would it be a good idea to somehow after user success to redirect back to localhost:3000 and now destroy the login view and render a user profile view? Could someone kind of give me an insight how I could route like that, I have a general idea but not so sure how to do that when I think the server side would need to communicate with the client side, or maybe not?

If the above is a bad idea, can someone give me advice on how to better look at this?

Edit: Routes

app.get('/', function(req, res) {
    res.render('index'); 
});

app.get('/login', function(req, res) {
    res.render('login', { message: req.flash('loginMessage') }); 
});

app.get('/profile', isLoggedIn, function(req, res) {
    res.render('profile', { user : req.user });
    res.redirect('/');
    io.sockets.on('connection', function (socket) {
        socket.userName = req.user.local.email;
        users[socket.userName] = socket;
        console.log(Object.keys(users))
    });
});
Michael Joseph Aubry
  • 12,282
  • 16
  • 70
  • 135
  • base `/` server route first check `isLogin`, if true render `profile`, false render/direct to `/login`. once logon redirect to `/` – wayne Mar 26 '14 at 00:50
  • Yeah I tried that, so I get the user is saved in the session. But when redirected to `/` how to I pass the users properties to the template. If I redirect to `/` it would use my index.handlebars which contains two buttons, login or sign up. – Michael Joseph Aubry Mar 26 '14 at 01:18
  • I mean your index.handlerbar need to be profile page. your current index become login.handlerbar. – wayne Mar 26 '14 at 01:50
  • Oh makes sense I wasn't thinking like that. Thanks let me try to change it up. – Michael Joseph Aubry Mar 26 '14 at 02:03
  • Okay I get what you're saying to an extent. My problem is if I set my default route, whenever I route back to that it runs the initial route. So by routing back to `/` it runs whatever is set. – Michael Joseph Aubry Mar 26 '14 at 02:40
  • I mean for example even if I `app.get('/', function(req, res) { res.render('login'); });` This is cool, but when I redirect after `isLoggedIn` itll re-render the login. – Michael Joseph Aubry Mar 26 '14 at 02:42

1 Answers1

1

use session to remember the login or use some custom function to query the status of login.

app.get('/', function(req, res) {

    if (req.session.isLogin) {
        res.render('profile', { user : req.user });
        // put other code here
    }
    else
        res.redirect('/login'); 
});

app.get('/login', function(req, res) {
    res.render('login', { message: req.flash('loginMessage') }); 
});
wayne
  • 3,410
  • 19
  • 11
  • Oh great, I'm new to routing and all this stuff. I appreciate the example. Glad you responded I was fiddling my sanity away :) – Michael Joseph Aubry Mar 26 '14 at 03:26
  • My route middleware looks like this `function isLoggedIn(req, res, next) { if (req.isAuthenticated()) return next(); res.redirect('/'); }` But `req.session.isLoggedIn` seems to return undefined? – Michael Joseph Aubry Mar 26 '14 at 03:34
  • So since I am using passport.js it seems I needed to use `req.user` it works, not sure if its safe?? but http://stackoverflow.com/questions/18739725/how-to-know-if-user-is-logged-in-with-passport-js – Michael Joseph Aubry Mar 26 '14 at 04:14
  • Is `if (req.user)` a good solution instead of `if (req.session.isLogin)` sound off and ill for sure give you a green check. Thanks for the insight.... – Michael Joseph Aubry Mar 26 '14 at 04:16
  • did you use passport.js for auth? or I mistaken from any other question. – wayne Mar 26 '14 at 05:17
  • Yeah I am not an expert by any means so take it with a grain of salt when I say "yes I did". I followed this awesome tutorial http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local my understanding has tremendously grown, I am pretty confident passport handles the auth. – Michael Joseph Aubry Mar 26 '14 at 18:28
  • I should have definitely mentioned that in the question. I see passport does handle sessions `app.use(passport.session()); // persistent login sessions` so yeah seems like this problem is solved. I liked your answer because the routing scheme is perfect. It works great thanks!!! – Michael Joseph Aubry Mar 26 '14 at 18:29