const admin = require('firebase-admin');

async function isAdmin(req, res, next) {

    const idToken = req.header('access-token');

    try {
        const claims = await admin.auth().verifyIdToken(idToken);
        if(claims.role !== 'admin') {
            res.status(403).send({error: `Access Denied! You must be an Admin.`});
        } else {
            next();
        }
    } catch(e) {
        res.status(500).send({error: `Something went wrong`});
    }

}

module.exports = isAdmin;
