Linkedin护照Oauth failureRedirect

下午好,

我在一个节点应用程序中工作。 具体来说,我正在使用“passport-linkedin-oauth2”。

有我的代码。

LinkedIn / index.js

'use strict'; var express = require('express'); var passport = require('passport'); var auth = require('../auth.service'); var router = express.Router(); router .get('/', passport.authenticate('linkedin', { state: 'comienzo' }), function(req, res){ // The request will be redirected to Linkedin for authentication, so this // function will not be called. }) .get('/callback', passport.authenticate('linkedin', { failureFlash : true, failureRedirect: '/login' }), auth.setTokenCookie); module.exports = router; 

LinkedIn / passport.js

 var passport = require('passport'); var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy; var models = require('../../api'); exports.setup = function (User, config) { passport.use(new LinkedInStrategy({ clientID: config.linkedin.clientID, clientSecret: config.linkedin.clientSecret, callbackURL: config.linkedin.callbackURL, scope: [ 'r_basicprofile', 'r_emailaddress'], state: true }, function(accessToken, refreshToken, profile, done) { process.nextTick(function () { // To keep the example simple, the user's LinkedIn profile is returned to // represent the logged-in user. In a typical application, you would want // to associate the LinkedIn account with a user record in your database, // and return that user instead. return done(null, profile); }); models.User.findOrCreate({ where: { linkedin: profile.id }, defaults: { name: profile.displayName, linkedin: profile.id, mail: profile.emails[0].value, password: 'xxxxxxx', role: 'admin', provider: 'linkedin', activo: true } }).spread(function (user, created) { console.log("x: " +user.values); return done(null, user) }).catch(function (err) { console.log('Error occured', err); return done(err); }); } )); }; 

我面临的问题是,我很确定LinkedIn正在logging正确。

在我的应用程序,当我按loginbuttonredirect到LinkedIn的网页,我填写的信息,然后我的服务器收到这个答案

 GET /auth/linkedin/callback?code=AQTfvipehBLAXsvmTIl1j3ISYCzF03F-EilhiLlfSJNqiwfQsyHeslLONOWY12Br-0dfV1pgkSSpCKlmtpiMVUCufJlatEBswWqfPe6iahoRF8IHIhw&state=comienzo 302 4ms - 68b 

我认为这意味着它是好的,因为我得到了我之前发送到LinkedIn API的状态和代码。

无论如何,我每次login总是redirect我的login页面,这是failureRedirect:'/login'(我已经testing,如果我改变这个路线,应用程序redirect我在这个属性点的地方)

此外,我已经检查过,它永远不会执行在linkedin用户的数据库search代码。

删除处理程序或策略实例化的状态属性,我不知道为什么,但是这解决了这个问题。

 exports.setup = function (User, config) { passport.use(new LinkedInStrategy({ clientID: config.linkedin.clientID, clientSecret: config.linkedin.clientSecret, callbackURL: config.linkedin.callbackURL, scope: [ 'r_basicprofile', 'r_emailaddress'], state: true // <-- Remove state from here }) } 

和这个代码

 router .get('/', passport.authenticate('linkedin', { state: 'comienzo' // <-- Or Remove state from here }), 

您可以将其设置为其中一个位置的状态,但不能同时设置,请删除其中的一个