Skip to content

Instantly share code, notes, and snippets.

@SiddharthManthan
Created May 13, 2024 16:00
Show Gist options
  • Save SiddharthManthan/6b37eda292d66b8ba4ae5b4c62734446 to your computer and use it in GitHub Desktop.
Save SiddharthManthan/6b37eda292d66b8ba4ae5b4c62734446 to your computer and use it in GitHub Desktop.
const jwt = require('jsonwebtoken');
const User = require('../models/User');
const requireAuth = (req, res, next) => {
const token = req.cookies.jwt;
// Check json web token exists and is verified
if(token) {
jwt.verify(token, process.env.JWTSECRET, (err, decodedToken) => {
if(err) {
console.log(err.message);
res.redirect('/authentication/signin.html');
return;
} else {
next();
}
});
} else {
res.redirect('/authentication/signin.html');
return;
}
}
module.exports = { requireAuth };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment