2018-01-29

PassportJS - Failed to find request token in session

banner

I was working with PassportJS to authenticate to Meetup using Meetup strategy. But this error drove me insane for hours.

Error: Failed to find request token in session

I'll show you one of the causes and how to fix it.

Prerequisite

There are many good tutorials on how to set up PassportJS so I will skip on this. Refer to these tutorials on how to setup PassportJS.

You can follow along, source code is available on GitHub (refer to README on how to run it)

Error Message

This is the full error message returned while trying to authenticate with Meetup.

https://gist.github.com/dance2die/91ffa2df32739d522abbaf25c4591e7e

Here is the video of error generated

https://www.youtube.com/watch?v=n70UQ9fSa7k&feature=youtu.be

Culprit

The error occurred due to Expression Session cookie.secure value.

https://gist.github.com/dance2die/200ed52cac8bc807757fc0e6449378f1

According to Expression Session documentation setting cookie.secure to true requires an HTTPS enabled site. But it's rarely the case that your development server is HTTPS enabled.

You could set the cookie.secure value to false while developing and set it to true for production by checking process.env.NODE_ENV.

But you can do better by setting cookie.secure value to a non-Boolean value, auto, which will automatically set to true or false depending on the security of current connection.

https://gist.github.com/dance2die/1f214f773f0661f8e3ac6f5a4327400d

Running the updated code

Let's update the cookie.secure value to auto and try again.

https://youtu.be/Lz8GBhhqFC4

And :awyeah:

,

it works 🎉

Conclusion

I've only discussed one possible cause for "Error: Failed to find request token in session" error message.

There are literally dozens of causes and fixes available when you Google it.