gpt4 book ai didi

angularjs - Node 中的错误 : 401 (Unauthorized) , Passport 身份验证

转载 作者:行者123 更新时间:2023-12-03 08:23:25 25 4
gpt4 key购买 nike

这里使用了 Passport 身份验证中间件,从登录 api 我生成了一个 jwt token 并将其发送到 API http://localhost:3030/home/在 header 中但它抛出

错误

angular.js:12587 POST http://localhost:3030/home/ 401 (Unauthorized).

angular.js:14525 Possibly unhandled rejection: {"data":"Unauthorized","status":401,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://localhost:3030/home/","headers":{"Accept":"application/json, text/plain, /"}},"statusText":"Unauthorized"}

路由.js

module.exports = function(app , passport){
app.use('/login',userCtrl);
app.use('/register',registerCtrl);
app.use('/home', passport.authenticate('jwt', { session: false }) , homerCtrl);
};

passposrt.js

var JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require("passport-jwt").ExtractJwt;
var config = require('./appConfig.js');

module.exports = function(passport){
var jwtOptions = {}
jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeader();
jwtOptions.secretOrKey = config.secret;
var strategy = new JwtStrategy(jwtOptions, function(jwt_payload, next) {
var user = users[_.findIndex(users, {id: jwt_payload.id})];
if (user) {
next(null, user);
} else {
next(null, false);
}
});
passport.use(strategy);
}

家庭 Controller

var mongoose = require("mongoose");
var User = mongoose.model("user");
var express = require("express");
var jwt = require('jsonwebtoken');
var config = require('./../config/appConfig.js');

//Type 1 using router
var router = express.Router();
router.post('/' , function(req, res, next){
var formData = req.body;
getUserData(formData , function(user){
res.json({ "responseMessage" : "Home Api Sucess" });
});
});

function getUserData(formData , cb){
User.findOne({username : formData.username}).then(function(user){
cb(user);
}).catch(function(err){
cb(err);
});
}

module.exports = router;

登录 Controller

var router = express.Router();
router.post('/' , function(req, res, next){
var formData = req.body;
var username = req.body.username;
var password = req.body.password;

getUserData(formData , function(user){
if(user){
user.verifyPassword(password , function(err, isMatch){
if(isMatch){
var token = createToken(user);
res.json({ "responseMessage" : "Login Sucess" , "token" :token});
}else{
res.json({ "responseMessage" : "Login Failed" });
}
})
}else{
res.json({ "responseMessage" : "Login Failed" });
}
});
});

function createToken(user){
var payload = {id: user.name};
var token = jwt.sign(payload, config.secret);
return "JWT " +token;
}

function getUserData(formData , cb){
User.findOne({username : formData.username}).then(function(user){
cb(user);
}).catch(function(err){
cb(err);
});
}

最佳答案

passport.js 中替换

jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeader();

jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('JWT');

关于angularjs - Node 中的错误 : 401 (Unauthorized) , Passport 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43869151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com