I’m working on the pinterest clone and trying to integrate passport-twitter.
Login seems to work in firefox (but not chrome or chromium). It redirects to twitter and returns to app when authorize button is clicked. However routes other than twitter (auth and callback) do not call deserializeUser and req.user is undefined.
I have looked at a number of examples but can’t see what I am doing wrong.
Shouldn’t your authentication route be set to /auth/callback/twitter instead of /callback/twitter ?. The reason I am saying that that is because the call back that you are pointing to in you twitter strategy is https://knik-fcc-pclone.herokuapp.com/auth/callback/twitter that is to say auth folder is specified from the root. May not fix your problem but at least I know that I had the call back and get routes matched in my projects for passport-twitter to work…
Thanx for your time Dereje, I will certainly take a look at your repo.
I’m very new to this, but as I understand it, the following code prefixes all routes (in that module) with /auth:
app.use('/auth', authRoutes);
e.g. in authRoutes “const authRoutes = require('./routes/auth');” all routes will be prefixed with /auth, so /callback/twitter becomes /auth/callback/twitter.
The auth flow itself seems to work. It’s keeping hold of the auth in other routes (e.g. /auth/user) that is missing.
Without downloading your whole project and checking the console.log at different steps it is really hard for me to see what is wrong, especially since I’m also a beginner at this, however one difference I see between mine and yours is that in the callback for the passport-twitter strategy mine has a process.nextTick(function() , which basically waits for the data to come back from twitter before firing off the database commands.
The other thing I see is that you are using a findOneAndUpdate command , but I am not sure what exactly you are updating, all the user information is provided by twitter, so either that twitter user already exists in your database, in which case you simply return the user, or the user does not exist, in which case you would create a new user in your db, I am not sure why an update would even be necessary here.
I noticed the process.nextTick as well, but it didn’t solve the issue unfortunately.
The findOneAndUpdate is so the user is created first time. It’s also left over from my base local auth template allowing the user to change their details/password.
Thanx for the link to scotch.io, will check it out.