nodejs - jwt 身份验证后 res.render 不起作用

问题描述

首先,我为我糟糕的英语道歉。

当您在主页面上按下 Write 按钮时,您想转到 writeBoard 页面

仅当您已登录并在 auth_login.js 中验证 jwt 时才转到 writeBoard.ejs。

然而,在 jwt 认证后 res.render 不起作用。

有什么问题吗?

main.js

app.get('/writeBoard',authMWRouter,(req,res) => {
    res.render('./basicBoard/writeBoard');
})

auth_login(authMWRouter)

const jwt = require('jsonwebtoken');
const { Account } = require('../models');

module.exports = (req,res,next) => {
    console.log(3)
    const { authorization } = req.headers;
    const [tokenType,tokenValue] = authorization.split(' ');

    if (tokenType != 'Bearer') {
        res.status(400).send({
            result: "fail",modal_title: "로그인 필요",modal_body: "로그인을 해주세요."
        });
        return;
    }

    try {
        const { nickname } = jwt.verify(tokenValue,'DongGyunKey');

        Account.findByPk(nickname).then((account) => {
            res.locals.account = account;
            next();
        });

        console.log(1)
    } catch (err) {
        res.status(400).send({
            result: "fail",modal_body: "로그인을 해주세요."
        });
        return;
    }
}

basicBoard.ejs(我的主页)

            function move_writeBoard() {
                const write_ajax = new XMLHttpRequest();
                var myModal = new bootstrap.Modal(document.getElementById("noticeModal"),{});

                write_ajax.onload = () => {
                    if (write_ajax.status == 400 || write_ajax.status == 401) {

                        responseTxt = JSON.parse(write_ajax.responseText);

                        const modalTitle = document.querySelector('#msgTitle');
                        var mtTxt = document.createTextNode(responseTxt['modal_title']);
                        modalTitle.appendChild(mtTxt);

                        const modalBody = document.querySelector('#msgbody');
                        var mbTxt = document.createTextNode(responseTxt['modal_body']);
                        modalBody.appendChild(mbTxt);

                        document.getElementById('exitButton').setAttribute('onclick','window.location.href="/login"');
                        document.getElementById('correctButton').setAttribute('onclick','window.location.href="/login"');
                        myModal.show();
                    }
                }

                write_ajax.onerror = () => {
                    console.error(write_ajax.responseText);
                }


                write_ajax.open('GET','/writeBoard');
                write_ajax.setRequestHeader('authorization','Bearer ' + localStorage.getItem("token"));
                write_ajax.setRequestHeader('Content-Type','application/json');
                write_ajax.send();
            }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)