简体   繁体   中英

Passport facebook authentication not working

I have a nodeJS application (express), which has a facebook login. I'm using the passport-facebook authentication, described in here passport-facebook . My login was still working couple weeks ago ok, but now something has changed. I'm not sure if Facebook has changed something, or if I have. I haven't touched to any of the login code, so I suspect it is something in the facebook's end.

Here is my authentication code:

router.get('/', passport.authenticate('facebook'), function(req, res){
});

router.get('/auth/facebook/callback', passport.authenticate('facebook', {
  failureRedirect: '/' }),
  function(req, res) {
  res.redirect('/calendar');
});

Basically, I want the app to automatically authenticate the user from the root of the app, and then redirect after the authentication to /calendar.

The symptons are following: If I try to go to the root and I haven't logged in with facebook earlier, then it will take me to the normal facebook login screen, as it should. But then on that screen, there is a warning of:

Given URL is not permitted by the Application configuration One or more of the given URLs is not permitted by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.

I checked all questions related to this, and it seems it is because I haven't configured the app domain and site url properly in the facebook app configuration. But they should be all fine: app domain: localhost, site url: http://localhost:3000/

So I don't know why it is complaining about it? I do not have any subdomains. The only routes I do have are: (/, /calendar, /calendar/events). The last /calendar/events routes are ajax routes. Anyways, after I enter my facebook credentials and login, then the page will redirect me to a empty page, with url: ( https://www.facebook.com/login.php?login_attempt=1 ). I opened the developer's tools, and I see one error message in its console:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

But, now I am actually logged in. Because if I now go to root or to /calendar, it will take me to correct page and show I'm logged in properly. So apparently the log in works, but there are some issues with redirection and allowed URL's. I just don't seem to know what and why. I'm using mongodDB as database.

I have no idea why this even worked, but I found a solution. Previously, I had configured valid OAuth redirect URI to be http://127.0.0.1:3000/auth/facebook/callback , and it was working all good. Now I changed it to use localhost instead of 127.0.0.1:3000, and that seemed to do the trick. Now everything is working as expected, though I don't know why. If someone is able to clearly tell me why, I will mark that as the correct answer.

It could be because of what site you have registered in your app. If you have registered it to http://localhost:3000/ you should work with domain localhost that for the callback. And if you have registered it as http://127.0.0.1:3000/ then you should work with 127...

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