express authorization middleware
const express = require('express');
const jwt = require('jsonwebtoken');
app.set('view engine', 'ejs');
// Simple page that a user must be authorized to access
// using JSON web tokens:
app.get('/lockedpage', authToken, (req, res) => {
res.render('lockedpage', { user: req.user });
});
function authToken(req, res, next) {
const token = req.cookies['auth-token'];
if (token == null) return res.redirect('/login');
jwt.verify(token, process.env.ACCESS_SECRET, (err, user) => {
if (err) return res.redirect('/login');
req.user = user;
next();
})
}