Code Monkey home page Code Monkey logo

Comments (2)

MRUNAL727 avatar MRUNAL727 commented on August 17, 2024

Remove "next" from line number 18

from youtube2022.

MadhavAgarwal01 avatar MadhavAgarwal01 commented on August 17, 2024

The issue is with the callback function for VerifyToken in verifyUser and verifyAdmin. The "next" parameter must be removed from the verifyToken function calls (not from the verifyToken's definition) to prevent it from directly going to the next middleware.

import jwt from "jsonwebtoken";
import createError from "../utils/error.js";

export const verifyToken = (req, res, next) => {

    console.log("VerifyToken Called!");

    const token = req.cookies.access_token;
    if (!token) return next(createError(401, "You are not authenticated!"));

    jwt.verify(token, process.env.JWT, (err, user) => {
        if (err) return next(createError(403, "Token is not valid!"));
        req.user = user;
        next();
    })
}

export const verifyUser = (req, res, next) => {

    console.log("VerifyUser Called!");

    verifyToken(req, res, () => {

        console.log("VerifyUser Callback function called!")

        if (req.user.id === req.params.id || req.user.isAdmin) {
            console.log("User verified!");
            next();
        } else {
            return next(createError(403, "You are not authorized!"));
        }
    });
};

export const verifyAdmin = (req, res, next) => {

    console.log("VerifyAdmin Called!");

    verifyToken(req, res, () => {

        console.log("VerifyAdmin Callback function called!")

        if (req.user.isAdmin) {
            next();
        } else {
            return next(createError(403, "You are not authorized!"));
        }
    });
};

from youtube2022.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.