10

So I tried to implement Facebook Login feature in my nodejs backend server. For testing purpose, I am trying client side to check the login and get access token. For that, I followed the docs and it says to use Javascript SDK and I followed the procedure, but there is a problem.

window.fbAsyncInit = function() {
    FB.init({
      appId            : '##############',
      autoLogAppEvents : true,
      xfbml            : true,
      version          : 'v4.0'
    })


    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });

  }

This is the code where I want to get the user login status, but I get an error saying:

The method FB.getLoginStatus can no longer be called from http pages

I am very much aware about the production rules, but this is development mode I am working on and still getting the error. Anything I am missing? Anything I have to do? I am running it on localhost and getting the error.

JustABeginner
  • 675
  • 1
  • 9
  • 25

5 Answers5

5
andyrandy
  • 70,918
  • 8
  • 110
  • 125
4

There's an easier fix if using create-react-app: set environment variable HTTPS to true. Cange your start script in package.json:

"scripts": {
  "start": "HTTPS=true react-scripts start",
  ...
}

If already using a custom script, you can also add it there:

process.env.HTTPS = true;

The main advantage of the latter method is that you can add a comment to explain it.

More info on configuration in CRA: https://create-react-app.dev/docs/advanced-configuration

wvdz
  • 16,053
  • 4
  • 47
  • 86
  • 2
    "set HTTPS=true" has no effect, as far as I can tell. – Marc Jul 10 '20 at 07:44
  • 1
    @Marc looks like I mistakenly assumed this to be nodeJS feature, but it's actually a feature of Create React App (https://create-react-app.dev/docs/advanced-configuration). I'll update my answer to reflect this. – wvdz Jul 13 '20 at 13:00
  • All you need to do is to add `HTTPS=true` in `.env` file and then simply run `npm start`. No need to change `npm start` script also – Germa Vinsmoke Feb 05 '21 at 14:06
  • @GermaVinsmoke That's what I'm saying... you don't need to do both. I'm just providing two different ways of setting the env variable. – wvdz Feb 17 '21 at 10:23
3

To solve this problem "The method FB.getLoginStatus can no longer be called from http pages" i used application https://ngrok.com/

and its make https in my browser on localserver via tunneling or sth like that

Windows 10 +wsl(Debian)

  1. Download ngrok

  2. Unzip it to folder

  3. Run cmd and navigate to folder with ngrok

  4. Now You can run :"ngrok http:80" <-assuming that You have apache2 service working

  5. I have my project in different/sub folder of DocumentRoot and i need also do this:

    https://stackoverflow.com/a/51114684

Jakub Ujvvary
  • 391
  • 3
  • 13
0

In package.json I just used this:

"serve": "vue-cli-service serve  --https true",

Instead of:

"serve": "vue-cli-service serve",

And it worked fine.

-2

You need an SSL-certificate deployed on your server (i.e. localhost). It will reject anything from an insecure location.

blackforest-tom
  • 428
  • 4
  • 8
  • and how exactly am I going to do that: ? – JustABeginner Oct 10 '19 at 15:20
  • Are you on Linux or on Windows (where the client runs on)? There are multiple solutions for this, all of them requiring to setup an http server (e.g. nginx or apache). You will find detailed instructions/tutorials via google how to set this up. I'm using nginx on WSL myself on my windows working machine for this purpose with self-signed openssl certificates. – blackforest-tom Oct 17 '19 at 09:30