{"size": 846, "ext": "js", "max_stars_repo_path": "src/middlewares/isValidated.js", "max_stars_repo_name": "Uthaan-IIITM/uthaan-backend", "max_stars_repo_stars_event_min_datetime": "2021-11-06T22:00:47.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-07T12:20:01.000Z", "max_issues_repo_path": "src/middlewares/isValidated.js", "max_issues_repo_name": "Uthaan-IIITM/uthaan-backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/middlewares/isValidated.js", "max_forks_repo_name": "Uthaan-IIITM/uthaan-backend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 8, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.6363636364, "max_line_length": 75, "alphanum_fraction": 0.6028368794}
const jwt = require('jsonwebtoken');
const admin = require('../models/admin.js')
const { JWT_KEY } = require('../config/env.js')

const isValidated = async(req, res, next) => {

    //Verify the token
    const verifiedToken = jwt.verify(req.headers.token, JWT_KEY)

    //If token is not valid,then send a response
    if (!verifiedToken) {
        return res.send({
            status: 404,
            message: "Invalid token"
        }).end()
    }

    //If token is valid, then search for admin in the database
    const adminExists = await admin.findOne({ email: verifiedToken.email })

    //If admin is not found, then send a response
    if (!adminExists) {
        return res.send({
            status: 404,
            message: "Admin not found"
        })
    }

    //Move to next function
    next()
}

module.exports = isValidated