如何解决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 举报,一经查实,本站将立刻删除。