Check whether or not a user is admin

Viewed 20

I'm trying to make it so that when a user has admin he should have access to the router. But unfortunately, it does not work, I'm getting an error that isAdmin is not defined, but I have it in the database.

function isAdminLogged(req, res, next) {
    if (req.User.isAdmin==="true") return next();
    res.redirect('/admin');
}

//ROUTES
app.get('/admin', isAdminLogged, (req, res) => {
    res.render("admin", { title: "Admin Area" });
})

Also, I would love to make it when a user is an admin he can see a div in an index.hbs

<div class="home">
    <h1>Home</h1>
    <a href="/logout">Logout</a>
</div>

{{#if user.isAdmin}}
<div>
    <h1>Hello Guys</h1>
</div>
{{/if}}

I'm new to express and I'm trying my best to understand, thank you in advance!

1 Answers

I have built authentication, and using mongodb but EJS template instead of Handlebars. I think if you go through it then that will help you a lot.
Here is the link to my repo CAMP.
The information that you have provided may be not sufficient to solve your issue from my side.

Related