Skip to content

Instantly share code, notes, and snippets.

@SiddharthManthan
Created May 13, 2024 16:00
Show Gist options
  • Select an option

  • Save SiddharthManthan/6b37eda292d66b8ba4ae5b4c62734446 to your computer and use it in GitHub Desktop.

Select an option

Save SiddharthManthan/6b37eda292d66b8ba4ae5b4c62734446 to your computer and use it in GitHub Desktop.

Revisions

  1. SiddharthManthan created this gist May 13, 2024.
    24 changes: 24 additions & 0 deletions authMiddleware.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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 };