1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Role base access control in NodeJs

Discussão em 'Angular' iniciado por Manish Choudhary, Junho 23, 2025.

  1. I'm a fresher and I got a task that In our project, we have 7 roles i.e

    1. Admin
    2. Insurer
    3. Investor
    4. Customer
    5. Moderator
    6. Lender
    7. Agent

    Admin can access all API but some of the API can access by Admin and moderator or Admin or Insurer or Admin or Investor and so on. There might be 30+ permutations and combinations. Is there any simple way to do that? Any dynamic way to do please help.

    The code I wrote this is middleware

    const isUser = (req, res, next) => {
    if (req.user && req.roles[0].toUpperCase == 'USER') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isInvestor = (req, res, next) => {
    if (req.user && req.roles[0].toUpperCase == 'INVESTOR') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isModerator = (req, res, next) => {
    if (req.user && req.roles[0].toUpperCase == 'MODERATOR') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isAdmin = (req, res, next) => {
    if (req.user && req.roles[0] == 'ADMIN') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isInsurer = (req, res, next) => {
    if (req.user && req.roles[0].toUpperCase == 'INSURER') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isNominee = (req, res, next) => {
    if (req.user && req.roles[0].toUpperCase == 'NOMINEE') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isAgent = (req, res, next) => {
    if (req.user && req.roles[0].toUpperCase == 'AGENT') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isLender = (req, res, next) => {
    if (req.user && req.roles[0].toUpperCase == 'LENDER') {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isAdminOrInvestor = (req, res, next) => {
    if (req.user && (req.roles[0].toUpperCase == 'INVESTOR'||req.roles[0].toUpperCase == 'ADMIN')) {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };
    const isAdminOrInsurer = (req, res, next) => {
    if (req.user && (req.roles[0].toUpperCase == 'INSURER'||req.roles[0].toUpperCase == 'ADMIN')) {
    next();
    } else {
    return res.status(401).send({
    message: `You don't have correct access`,
    });
    }
    };

    Continue reading...

Compartilhe esta Página