使用角度$ http请求时,护照req.isAuthenticated始终返回false [英] passport req.isAuthenticated always return false when using angular $http request
问题描述
我正在使用passportjs进行用户身份验证.
I am using passportjs for my user authentication.
使用邮递员,我可以成功登录,并且req.isAuthenticated始终在登录后的子序列请求中返回true.
By using postman, I can successfully login and req.isAuthenticated always returns me true in the subsequence requests after login.
通过使用角度$ http ,但是,登录正常,但是req.isAuthenticated始终在子序列请求中返回false.我的角度应用程序(localhost:3000)和节点应用程序(heroku)位于不同的域.我的想法是它可能与CORS或会话cookie有关,因为我在浏览器中看不到任何cookie.
By using angular $http, However, login works ok but the req.isAuthenticated always returns me false in the subsequence request. My angular app(localhost:3000) and the node app(heroku) are at different domain. My thoughts was it may relate to either CORS or session cookie since I do not see any cookies in my browser.
我做了什么
- 我尝试将请求标头设置为允许CORS.
- 我还尝试在有角度的$ http和节点应用程序中设置 Access-Control-Allow-Credentials .
不幸的是,所有尝试都失败了T_T
Unfortunately, all attempts fail T_T
Nodejs设置
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
});
passport.serializeUser(function(account, done) {
done(null, account.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
Account.findById(id, function(err, account) {
done(err, account);
});
});
passport.use('local-signup', new LocalStrategy({
usernameField: 'email'
}, function(email, password, done) {
....
}));
passport.use('local-login', new LocalStrategy({
usernameField: 'email'
}, function(email, password, done) {
....
}));
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser(config.app.sessionSecret));
app.use(session({
secret: config.app.sessionSecret,
resave: false,
saveUninitialized: true,
cookie: { httpOnly: true, maxAge: 2419200000 }
}));
// Passport for account authentication
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
///////////////////////////////////////////////////////////////////route controller function
authenticate: function(req, res) {
if (!req.isAuthenticated()) {
res.redirect('/login');
} else {
res.status(200).send('successful');
}
}
角度
$http.post('http://xxxxx/login',
{ email: $scope.user.email,
password: $scope.user.password
})
.then(function(response) {
...
}, function(response) {
...
});
$http.get('http://xxxxx/authenticate',
{ withCredentials: true }).success(function() {
...
})
.error(function() {
... // always get here with 302 redirect
});
我不明白的问题/东西
- 是因为未在浏览器中设置会话cookie导致了问题
- 如果与CORS有关,我会错过任何设置吗?
- 还有什么???
推荐答案
我根据@robertklep注释自行解决了此问题.基本上,passwordjs设置没有任何问题.这与如何以角度发送withCredentials标志有关.而不是调用$ http的get或post方法.我使用以下格式,并且对我有用
I solve the issue myself based on @robertklep comment. Basically, there is nothing wrong with passportjs settings. It is all about how you send the withCredentials flag in angular. Instead of calling get or post method of $http. I use the following format and it works for me
$http({
method: 'GET',
url: 'xxxx',
data: {...},
withCredentials: true
}).success ....
参考: https://docs.angularjs.org/api/ng/service/ $ http#usage
Reference: https://docs.angularjs.org/api/ng/service/$http#usage
这篇关于使用角度$ http请求时,护照req.isAuthenticated始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!