Login/logout changes in routes/index.mjs

This router module handles the home page. It does not require the user to be logged in, but we want to change the display a little if they are logged in:

router.get('/', async (req, res, next) => {  try {    let keylist = await notes.keylist();    let keyPromises = keylist.map(key => { return notes.read(key) });    let notelist = await Promise.all(keyPromises);    res.render('index', {       title: 'Notes', notelist: notelist,      user: req.user ? req.user : undefined    });  } catch (e) { next(e); }});

Remember that we ensured that req.user has the user profile data, which we did in deserializeUser. We simply check for this and make sure to add that data when rendering the views template.

We'll be making similar changes to ...

Get Node.js Web Development - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.