{"size": 3029, "ext": "js", "max_stars_repo_path": "controller/auth.js", "max_stars_repo_name": "Tobils/nodejs-login-JWT", "max_stars_repo_stars_event_min_datetime": "2020-06-21T08:51:14.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-09T04:14:53.000Z", "max_issues_repo_path": "controller/auth.js", "max_issues_repo_name": "Tobils/nodejs-login-JWT", "max_issues_repo_issues_event_min_datetime": "2022-03-26T03:10:15.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T03:10:15.000Z", "max_forks_repo_path": "controller/auth.js", "max_forks_repo_name": "Tobils/nodejs-login-JWT", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 34.4204545455, "max_line_length": 138, "alphanum_fraction": 0.5463849455}
const db = require('../model/db_config');
const db_auth = require('../model/db_auth');
const jwt = require('jsonwebtoken');
const fs = require('fs');
const dotenv = require('dotenv').config();
const jwtExpireSeconds = 300; // 300 detik masa berlaku token, ketika sudah habis... maka akan diminta login lg atau diminta refresh token


module.exports.isUserAuthorized = function(req, res, next) {
    const username = req.body.username;
    const password = req.body.password;
    var profile = {
        username: username,
        password: password
    }
    db_auth.dbAuth(username, password, (cb) => {
        if (cb) {
            let privateKEY = fs.readFileSync(process.env.private_key, 'utf-8');
            let token = jwt.sign({ username }, privateKEY, {
                algorithm: "RS256",
                expiresIn: jwtExpireSeconds
            });

            req.username = profile.username;
            req.token = token;
            return next();
        } else {
            return res.render('login', {
                pageTitle: "Login",
                contentTitle: "Username atau Password Salah !"
            });
        }
    });
}

module.exports.isTokenAuthorized = function(req, res, next) {
    const token = req.cookies['token'];
    /**
     * if cookies still exist let continue, if not please re-login
     */
    if (typeof token !== 'undefined') {
        console.log('token : ', token)
        console.log("request path : ", req.originalUrl)
        if (!token) {
            return res.status(401).end()
        }
        var payload;
        try {
            let publicKEY = fs.readFileSync(process.env.public_key, 'utf-8');
            payload = jwt.verify(token, publicKEY, { algorithm: "RS256" });
            console.log("username payload : ", payload);
            req.username = payload.username;
            return next();
        } catch (e) {
            if (e instanceof jwt.JsonWebTokenError) {
                // if the error thrown is because the JWT is unauthorized, return a 401 error or return to login page again
                // return res.status(401).end()
                return res.render('login', {
                    pageTitle: "Login",
                    contentTitle: "Username atau Password Salah !"
                });
            }
            // otherwise, return a bad request error or return to login page again
            // return res.status(400).end()
            return res.render('login', {
                pageTitle: "Login",
                contentTitle: "Username atau Password Salah !"
            });
        }
    } else {
        /**
         * bagaimaana agar tidak perlu login setiap cookies habis, tp cukup cek token nya sajah
         */
        return res.render('login', {
            pageTitle: "Login",
            contentTitle: "Username atau Password Salah !"
        });
    }
}


module.exports.isLogOut = (req, res, next) => {
    var token = req.cookies['token'];
    if (!token) {
        req.token = token;
    }
    next();
}