简体   繁体   English

Linkedin Passport Oauth失败重定向

[英]Linkedin Passport Oauth failureRedirect

Good afternoon, 下午好,

I'm working in a node application. 我正在节点应用程序中工作。 Concretely I'm working with "passport-linkedin-oauth2". 具体来说,我正在使用“ passport-linkedin-oauth2”。

There is my code. 有我的代码。

linkedin/index.js 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 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);
  });

}
));
};

The problem I'm facing is that I'm pretty sure that LinkedIn is logging properly. 我面临的问题是我很确定LinkedIn是否正确记录了日志。

In my app when i press login button it redirect me to LinkedIn webpage, I fill the information and then my server receives this answer 在我的应用程序中,当我按登录按钮时,它将我重定向到LinkedIn页面,我填写了信息,然后我的服务器收到了这个答案

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

I think that this means that it is ok because I get the state that I have sent to LinkedIn API before and the code. 我认为这意味着可以,因为我得到了之前发送给LinkedIn API的状态和代码。

Anyway, every time I login always redirect me to Login page which is failureRedirect: '/login' ( I have tested that if I change this route, the app redirect me where this attribute point) 无论如何,每次我登录时始终将我重定向到登录页面,该页面是failureRedirect:'/ login'(我已经测试过,如果我更改此路由,则应用会将我重定向到该属性指向的位置)

Also I have checked that it never executes the code that search in the db for the linkedin user. 我还检查了它是否从不执行在db中搜索linkedin用户的代码。

Remove the state property on the handler or at the strategy instantiation, i'm not sure why but this solves the issue. 删除处理程序或策略实例上的state属性,我不确定为什么,但这可以解决问题。

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
  })
}

and this code 和这段代码

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

You can just set it the state on one of this places but not both, so remove one of them 您可以仅在其中一个位置上将其设置为状态,但不能同时在两个位置上设置状态,因此请删除其中一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM