{"size": 842, "ext": "js", "max_stars_repo_path": "Middleware/loggedStudent.js", "max_stars_repo_name": "mohitdmak/OnlineAttendanceApi", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Middleware/loggedStudent.js", "max_issues_repo_name": "mohitdmak/OnlineAttendanceApi", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Middleware/loggedStudent.js", "max_forks_repo_name": "mohitdmak/OnlineAttendanceApi", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.1612903226, "max_line_length": 70, "alphanum_fraction": 0.5593824228}
// Requiring jwt verifier and secret string.
const jwt = require('jsonwebtoken');
const JWTsecret = require('../Config/JWTsecret');

// Requiring User model.
const Student = require('../models/Student');

// Middleware to establish user currently accessing the site.
const checkLoggedStudent = (req, res, next) => {
    const token = req.cookies.jwtAUTH;

    if(token){
        jwt.verify(token, JWTsecret, async (err, decodedToken) => {
            if(err){
                res.locals.student = null;
                next();
            }
            else{
                let student = await Student.findById(decodedToken.id);
                res.locals.student = student;
                next();
            }
        });
    }
    else{
        res.locals.student = null;
        next();
    }
}

module.exports = { checkLoggedStudent };