How to check user has logged in before every request - node.js

76 views
Skip to first unread message

prem Kumar

unread,
Feb 12, 2015, 7:43:13 AM2/12/15
to nod...@googlegroups.com
Hi,

Need to check whether the user has logged-in or not with session before every page request.. Please guide me to solve this issue..




Thanks,
Premkumar M.

Darshit Shah

unread,
Feb 12, 2015, 10:44:10 PM2/12/15
to nod...@googlegroups.com
Most systems use Session for that. You can store logged in user id in session and check in every request if that id is undefined? If yes then user is not logged in else user is logged in.
You need to set user id in session once user has successfully authenticated the system.

If you wish to achieve single sign in authentication using OAuth then you can use module called "passport".

zladuric

unread,
Feb 14, 2015, 4:05:37 PM2/14/15
to nod...@googlegroups.com
I tend to use token-based auth, but you can also use cookies to keep your users' sessions..

The principle is the same: hook a "middleware" function into the app, into the request, that checks if the user is logged in.
If yes, usually it will add a 'user' object with more info to the request.

Now, usually you don't do it on your own, something like passport.js is what people would use for this purpose.
Reply all
Reply to author
Forward
0 new messages