Reputation: 105
So I'm new at authentication. I just setup my login using Passport and now my user is supposed to be logged in.
router.post('/login/data',
passport.authenticate('local', {successredirect:'/', failureredir:'/login', failureFlash: true}),
function(req, res) {
// If this function gets called, authentication was successful.
// `req.user` contains the authenticated user.
res.redirect('/');
});
It redirected me to my homepage, but now I would like it that every page in my app is different depending on the user that is logged in. How do I render my pages based on the logged-in user's data?
Upvotes: 0
Views: 1580
Reputation: 1399
Inside the route for your homepage, you can check to see if there is a user authenticated, and if so, render a template with their data.
router.get('/', function(req, res){
if (req.user) {
res.render('index', { user: req.user });
} else {
res.redirect('/login');
}
});
Then inside your .ejs
file you will have access to the user's data.
<h1>Username: <%= user.username %></h1>
Upvotes: 2