E
E
EmKotlety2022-03-25 16:19:05
Node.js
EmKotlety, 2022-03-25 16:19:05

Why is the session not available in the middleware?

Why is the session visible in all files, but in the middleware net///???
AUTHMIDDLEWARE:

const jwt = require('jsonwebtoken');

module.exports = function (req, res, next) {
    if (req.method === "OPTIONS") {
        next()
    }
    try {
        const token = req.headers.authorization.split(' ')[1] ;// Bearer asfasnfkajsfnjk
       const email = req.session.email/////////UNDERFIND !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        console.log('ОБЪЕКТ EMAIL:', email )
       
        if (!token) {
            return res.status(401).json({message: "Не авторизован"})
        }
        const decoded = jwt.verify(token, process.env.SECRET_KEY)
        req.user = decoded
        next()
    } catch (e) {
        res.status(401).json({message: "Не авторизован"})
    }
};

And here is the route file where authmiddleware connects
const Router = require ('express');
const { check } = require('express-validator');
const userController = require('../Controllers/userController');
const validation = require('../MiddleWare/validation');
const authMiddleware = require('../MiddleWare/authMiddleware');
const router = new Router;

router.post('/registration',
validation.registration,
userController.registration)
router.post('/login',validation.login, userController.login)
router.get('/check', authMiddleware, userController.check)/// Подключаем сюда!!!!!!!
router.get('/activate/:link',   userController.activate)
router.get('/session', (req, res)=>{
    console.log('груша', req.session.email) /////А вот тут сессию видно
    console.log()
    res.send('EPIC Seesion...');
  })

module.exports = router;

controller is fully functional

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question