Question

I've started a project to build a small and simple web application in HTML5/Canvas/JS that allows users to graph simple sets of data.

I want to implement and user account/login system but I want to use JS to do this rather than traditional PHP solutions.

I've been following this guide as an example. http://www.quietless.com/kitchen/building-a-login-system-in-node-js-and-mongodb/

My question is, after implementing a system like this, how does one treat/handle/deal with a user being either logged in or out in my code all the time? What are common and best practice appraoches for doing this?

Était-ce utile?

La solution

write a middleware function such as:

function needUser(req, res, next) {
    if (!req.session.user) {
        res.redirect('/login');
        return;
    }
    next();
}

Any routes that are publicly available to not-logged-in users can just ignore this and be set up as normal. Any routes that require a logged-in user can include this as a middleware.

app.get('/inbox', needUser, inboxRoute);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top