简体   繁体   中英

Linkedin Passport Oauth failureRedirect

Good afternoon,

I'm working in a node application. Concretely I'm working with "passport-linkedin-oauth2".

There is my code.

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

}
));
};

The problem I'm facing is that I'm pretty sure that LinkedIn is logging properly.

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

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.

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)

Also I have checked that it never executes the code that search in the db for the linkedin user.

Remove the state property on the handler or at the strategy instantiation, i'm not sure why but this solves the issue.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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