Uname: Linux business55.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64
Software: LiteSpeed
PHP version: 8.1.32 [ PHP INFO ] PHP os: Linux
Server Ip: 162.213.251.212
Your Ip: 18.221.207.166
User: allssztx (535) | Group: allssztx (533)
Safe Mode: OFF
Disable Function:
NONE

name : autovalidoter.js
const jwt = require('jsonwebtoken');
const JWT_SECRET = "l!TtLeGr()O//En";
const Adminpanel = require("../schema/Admin-panel")

const cookieAuth = () => {
    return async (req, res, next) => {
        try {
            const cookieAuthtoken = req.cookies['authtoken'];
            if (cookieAuthtoken) {

                const tokenAdmin = jwt.verify(cookieAuthtoken, JWT_SECRET)
                const dbAdmin = await Adminpanel.findById(tokenAdmin.id)
                const { _id, name, email } = dbAdmin
                const admin = {
                    id: _id, name, email
                }
                const authtoken = jwt.sign({ id: _id, email }, JWT_SECRET);
                res.cookie('authtoken', authtoken)
                req.admin = admin
                if(req.path.includes('login')){
                    return res.redirect('/sites/little-groovin-guitar/admin-panel/')
                }
                next()
            }
            else {
                if(!req.path.includes('login') || !req.path.includes('register')){
                    res.redirect('/sites/little-groovin-guitar/admin-panel/register')
                }else{
                    next()
                }
            }
        }
        catch (err) {
            console.log('req',req)
            if(!req.path.includes('login') && !req.path.includes('register')){
                res.redirect('/sites/little-groovin-guitar/admin-panel/login')
            }else{
                next()
            }
            console.log(err.message)
        }
    }
}

module.exports = cookieAuth
© 2025 GrazzMean-Shell