Как выполнять middleware по условию?

У меня авторизация с помощью email и с помощью google auth. Встал вопрос: когда пользователь загружает файл в чат и отправляет его серверу, мне нужно что бы мидлвейр проверял, залогинен ли пользователь. НУ и сам вопрос: Как проверять как залогинен пользователь, и как вызывать в этом случае определенный мидл?

class AuthMiddleware {
static auth (req,res,next) {
    if(req.method === 'OPTIONS'){
        next()
    }
    try{
        const token = req.headers.authorization.split(' ')[1];
        if(token === 'null'){
            return res.status(403).json({message: 'You are not logged in!'})
        }
        const decoded = jwt.verify(token, process.env.SECRET_KEY)
        req.user = decoded;
        next();
    }
    catch(err){
        console.log(err)
    }
}
static async authWithGoogle(req,res,next){
    try{
        const token = req.headers.authorization.split(' ')[1];
        const decode = await admin.auth().verifyIdToken(token)
        if(!decode){
            return res.status(403).json({message: 'You are not logged in! !'})
        }
        next()
    }
    catch(err){
        console.log(err)
    }
}

}

Роутер:

router.post('/upload',  'Тут должен быть мидлвейр', fileController.upload)

Ответы (0 шт):