使用JWT进行身份验证后,Nodejs不呈现视图

时间:2018-11-29 19:15:00

标签: node.js express jwt axios

nodejs不会呈现我的视图,而是在我的AJAX请求日期发送视图的HTML,而我不在乎它的全部含义。

没有错误发生,我已经测试了所有代码,对我来说似乎一切正确。

 axios.post('/login/authentication/', {
                username: usernameInput.value,
                password: userPasswordInput.value
            }).then(response => {
                axios.post('/home/v1/', null, { headers: { Authentication: response.data.token }}).then(response => {
                    console.log(response)
                }).catch(error => {
                    console.log(error)
                });
            }).catch(error => {});

JWT

 const jwt = require('jsonwebtoken');

module.exports.sign = (userId, expiresInValue) => {
    const userToken = jwt.sign({ userId }, process.env.SECRET, {
        expiresIn: expiresInValue // EXPIRA EM 5 MINUTO
    });

    return userToken;
}

module.exports.tokenAuthentication = (req, res, next) => {
    if(!req.headers. authentication){
        res.status(401).send({ 
            authStatus: false, 
            message: "NO TOKEN PROVIDED." 
        }).end();
    } else {
        jwt.verify(req.headers. authentication, process.env.SECRET, (error, decoded) => {
            if (error) {
                res.status(500).send({ 
                    authStatus: false,
                    message: "FAILED TO AUTHENTICATE TOKEN." 
                }).end();
            } else {
                req.body.userId       = decoded.userId;
                req.body.userToken    = req.headers. authentication;
                req.body.userTokenExp = "5 min";

                next();
            }
        });
    }
}

登录控制器

const { validationResult } = require('express-validator/check');
const jwtLibrary = require('../../librarys/others/JWT');

module.exports.redirectRouter = (res) => res.redirect('/login');
module.exports.login = (res) => res.render('login/view');

module.exports.authentication = (req, res) => {
    if(!validationResult(req).isEmpty()) {
        res.status(401).send({ 
            authStatus: false, 
            message: "WITHOUT AUTHORIZATION"
        }).end();
    } else {
        /** TEMPORÁRIO */
        if(req.body.username === "1" && req.body.password === "1"){
            const jwtToken = jwtLibrary.sign(req.body.username, 300);
            res.status(200).send({ 
                authStatus: true, 
                token: jwtToken, 
                message: "AUTHORIZED"
            }).end();
        } else {
            res.status(401).send({ 
                authStatus: false, 
                message: "WITHOUT AUTHORIZATION"
            }).end();
        }
        //
    }
}

HOME ROUTER

module.exports = (app) => {
    app.route('/home/v1/')
    .get(jwtLibrary.tokenAuthentication, (req, res) => {
        app.controllers.home.controller.home(req, res);
    });
}

家庭控制器

const jwtLibrary = require('../../librarys/others/JWT');

module.exports = (app) => {
    app.route('/home/v1/').post(jwtLibrary.tokenAuthentication, (req, res) => {
        app.controllers.home.controller.home(req, res);
    });
}

enter image description here

0 个答案:

没有答案