Answers for "express authorization middleware"

0

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();
  })
}
Posted by: Guest on July-24-2021

Code answers related to "express authorization middleware"

Code answers related to "Javascript"

Browse Popular Code Answers by Language