如何将jsonwebtoken从带有jquery的localstorage发送到节点服务器。我试图用这种方式做到这一点,但它不起作用。这是我的客户端代码,用于处理请求。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row mt" id="addObjectForm">
<div class="col-sm-1">
<button type="button" id="addRowButton">Add Row</button>
</div>
</div>
在中间件中进行身份验证路由之后,我想从auth标头中获取令牌
$(function () {
$('.subForm').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/users/spec/register',
data: $(this).serialize(),
success:function(data){
if(data.success){
location.href="http://localhost:3000/login"
}else{
location.href="http://localhost:3000/signup"
}
}
});
e.preventDefault();
});
$('.personAuth').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/person/register',
data: $(this).serialize(),
success:function(data){
if(data.success){
location.href="http://localhost:3000/login"
}else{
console.log("Chexav");
location.href="http://localhost:3000/signup";
}
}
});
e.preventDefault();
});
$('.companyAuth').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/company/register',
data: $(this).serialize(),
success:function(data){
if(data.success){
location.href="http://localhost:3000/login"
}else{
location.href="http://localhost:3000/signup"
}
}
});
e.preventDefault();
});
$('.logInForm').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/users/authenticate',
data: $(this).serialize(),
success:function(data){
console.log(data);
if(data.token){
localStorage.setItem("Authorization",data.token);
$.ajax({
type:'get',
url:'http://localhost:3000/users/user',
beforeSend: function(xhr){xhr.setRequestHeader('auth', localStorage.getItem("Authorization"));},
success:location.href="http://localhost:3000/users/user"
})
}
}
});
e.preventDefault();
});
});
这是模型和函数与数据库
router.post('/authenticate', (req, res,next) => {
const email = req.body.email;
const password = req.body.password;
Model.findUser(email, (err, user) => {
if(err) throw err;
if(!user){
return res.json({success: false, msg: 'User not found'});
}
Model.comparePassword(password, user.password, (err, isMatch) => {
if(err) throw err;
if(isMatch){
let payload={
name:user.name,
email:user.email_num,
role:user.role,
deleted:user.deleted,
isActive:user.isActive,
created:user.created,
};
let token = jwt.sign(payload,config.JWT_SECRET,{
expiresIn:1440
});
Model.saveToken(email,user.role,token,(err,success)=>{
if(err) return err;
console.log("Success");
// res.cookie('Authorization',token);
res.json ({ success: true, token: token });
});
} else {
return res.json({success: false, msg: 'Wrong password'});
}
});
});
// res.redirect("/user");
});
router.use(function(req, res, next) {
// console.log(req.headers);
let token = req.headers['auth'];
console.log(typeof(req.headers['auth']));
if (token) {
jwt.verify(token, config.JWT_SECRET, function(err, decoded) {
if (err) {
console.log(err);
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
req.decoded = decoded;
next();
// res.render("user");
}
});
} else {
return res.status(403).json({
success: false,
message: 'No token provided.'
});
}
});
router.get("/user", (req,res)=>{
res.render("user");
});
我使用mongodb,我有三个集合,这个代码用于处理所有这些代码。
中间件中的console.log(typeof(req.headers ['auth']))在未定义之后带来字符串,我找不到这个的原因。我也知道我可以用cookie做到这一点,但我想要使用仅本地存储以另一种方式执行此操作。 感谢您的帮助和抱歉语言错误)))))答案 0 :(得分:0)
这对我有用:
在服务器端:
mod.cor2<-gls(resp~exp1,correlation=corGaus(form=~lat,nugget=TRUE))
mod.cor3<-gls(resp~exp2,correlation=corGaus(form=~lat,nugget=TRUE))
然后在前面发送:
router.use(function(req, res, next){
let token = req.headers["auth"];
console.log("Token 1 "+token);
req.decoded = token;
return next();
});
router.get("/a", function(req, res){
console.log("Token 2 "+req.decoded);
return res.status(200).json({});
});
良好的值在控制台中正确显示