{"size": 3731, "ext": "js", "max_stars_repo_path": "services/client/util/jwt.js", "max_stars_repo_name": "xhatem/AHD-socket.io-chat", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "services/client/util/jwt.js", "max_issues_repo_name": "xhatem/AHD-socket.io-chat", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "services/client/util/jwt.js", "max_forks_repo_name": "xhatem/AHD-socket.io-chat", "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": 33.6126126126, "max_line_length": 121, "alphanum_fraction": 0.3993567408}
const jwt = require('jsonwebtoken');
const jwtKey = "ksngklasjri21AHD#$$92123SECRET_Key213klasnfaljsbfasfjb&TYRIQUGLKJBFSAL82913!@$!@r87987sfa98*(YSFJhkjashd"

class AuthorizationService {
    async verifyToken(token){
        console.log('the icoming token is ',token);
        if(!token) 
            return {};
        return await new Promise((resolve,reject) =>
            jwt.verify(token,jwtKey,(err,decoded) => err ? reject(err) : 
                                                        resolve(decoded))
        );
    }

    // verifyAuthToken(req, res, next) {
    //     const bearerHeader = req.headers['authorization'];
    //     //check if bearer is undefined
    //     if (typeof bearerHeader !== 'undefined') {
    //         const bearerToken = bearerHeader.split(' ')[1];
    //         jwt.verify(bearerToken, jwtKey, async (err, authData) => {
    //             if (err) {
    //                 //console.log(req.token);
    //                 res.sendStatus(403);
    //             }
    //             //forbidden
    //             else {
    //                 req.user = authData;
    //                 req.token = bearerToken;
    //                 next();
    //             }
    //         })

    //     } else {
    //         res.status(403).json({
    //             token: null,
    //             message: "UNAUTHORIZED access"
    //         });
    //     }
    // }

    // verifyDoctor(req, res, next) {
    //     const bearerHeader = req.headers['authorization'];
    //     //check if bearer is undefined
    //     if (typeof bearerHeader !== 'undefined') {
    //         const bearerToken = bearerHeader.split(' ')[1];
    //         jwt.verify(bearerToken, jwtKey, async (err, authData) => {
    //             if (err) {
    //                 //console.log(req.token);
    //                 res.sendStatus(403);
    //             }
    //             //forbidden
    //             else {
    //                 if (authData.role == "Doctor") {
    //                     req.user = authData;
    //                     req.token = bearerToken;
    //                     next();
    //                 } else {
    //                     //only doctor can do this action
    //                     res.status(403).json({
    //                         message: "Only Doctor can do this action"
    //                     });
    //                 }

    //             }
    //         })

    //     } else {
    //         res.status(403).json({
    //             token: null,
    //             message: "UNAUTHORIZED access"
    //         });
    //     }
    // }

    // verifyOdoo(req, res, next) {
    //     const bearerHeader = req.headers['authorization'];
    //     //check if bearer is undefined
    //     if (typeof bearerHeader !== 'undefined') {
    //         const bearerToken = bearerHeader.split(' ')[1];
    //         jwt.verify(bearerToken, jwtKey, async (err, authData) => {
    //             if (err) {
    //                 //console.log(req.token);
    //                 res.sendStatus(403);
    //             }
    //             //forbidden
    //             else {
    //                 if (authData.role !== "Admin")
    //                     res.sendStatus(403);
    //                 else {
    //                     req.user = authData;
    //                     req.token = bearerToken;
    //                     next();
    //                 }


    //             }
    //         })

    //     } else {
    //         res.status(403).json({
    //             token: null,
    //             message: "UNAUTHORIZED access"
    //         });
    //     }
    // }


}

module.exports = new AuthorizationService();
