Lunchbox
Lunchbox

Reputation: 2156

Callback not being called for passport-amazon

I have a passport-amazon strategy on a route in an express server (I have stripped out the user handling code from the callback for brevity):

passport is initialized thusly in server.js:

//configure passport
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);

The passport strategy is added in require('./config/passport')(passport):

passport.use(
  new AmazonStrategy({
    clientID: process.env.AMAZON_CLIENT_ID,
              clientSecret: process.env.AMAZON_CLIENT_SECRET,
              callbackURL: process.env.AMAZON_CALLBACK_URL
          },
     (accessToken, refreshToken, profile, done) => {
       console.log('in the callback for amazon auth');
       //user handling code here
       return done(null, user);

);

the route:

routes.get(
        '/login',
        passport.authenticate(
            'amazon',
            {
                scope: ['profile']
            }
        ),
        (req, res) => {
          console.log('something happens here');
        }
      )

The authentication makes it to Amazon and the callback URL is used. The middleware I have on the route for the callback url:

function isLoggedIn(req, res, next) {
        if (req.isAuthenticated())
            return next();

        res.redirect('/');
}

is being called and req.isAuthenticated() is false. The passport callback function never logs to the console.

Upvotes: 0

Views: 271

Answers (2)

Lunchbox
Lunchbox

Reputation: 2156

I found the problem using the example code. I needed another authenticate call on the intermediate callback route:

routes.get(
      '/amazon/auth/callback', 
      passport.authenticate('amazon', {failureRedirect: '/'}),    
      (req, res) => {
        console.log("at /amazon/auth/callback");
        res.redirect('/highlights');
    });

This executes the passport callback and the isLoggedIn method passes.

Thanks to Ebrahim Pasbani for his help and sticking in there with me.

Upvotes: 0

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9406

Your isLoggedIn middleware is the bug. You shouldn't check for auth in callback url, because at that point there is no user yet.

You can redirect user to a secure route in callback route and use isLoggedIn middleware on that route.

Upvotes: 1

Related Questions