Implement Facebook API login with reactjs

后端 未结 10 2006
广开言路
广开言路 2020-12-04 09:25

I\'m working on using Facebook\'s Javascript SDK for authentication. I\'ve been able to import the SDK properly and put a Like button on my page. But, the facebook login but

10条回答
  •  隐瞒了意图╮
    2020-12-04 09:54

    I post my solution here, as the answers helped me to implement it, but the accepted answer from ritmatter did not work without the

    FB.Event.subscribe('auth.statusChange', function(response)
    

    statement from RubyFanatic's answer. Furthermore the "onlogin" confused me a lot. It is simply not required in my eyes.

    I want to share it, as this is a c&p solution, here my full code against fb api v2.8 (react-bootstrap can be kicked of course):

    /* global FB*/
    import React, { Component } from 'react';
    import { Grid, Row, Col } from 'react-bootstrap';
    
    export default class Login extends Component {
      constructor(props) {
        super(props);
        this.checkLoginState = this.checkLoginState.bind(this);
        this.handleClick = this.handleClick.bind(this);
        this.testAPI = this.testAPI.bind(this);
        this.statusChangeCallback = this.statusChangeCallback.bind(this);
      }
    
      componentDidMount() {
        window.fbAsyncInit = function() {
          FB.init({
            appId      : 'YOURAPIKEYHERE',
            cookie     : true,
            xfbml      : true,
            version    : 'v2.8'
          });
          FB.AppEvents.logPageView();
          FB.Event.subscribe('auth.statusChange', function(response) {
            if (response.authResponse) {
              this.checkLoginState();
            } else {
              console.log('---->User cancelled login or did not fully authorize.');
            }
          }.bind(this));
        }.bind(this);
    
        // Load the SDK asynchronously
        (function(d, s, id) {
          var js, fjs = d.getElementsByTagName(s)[0];
          if (d.getElementById(id)) return;
          js = d.createElement(s); js.id = id;
          js.src = '//connect.facebook.net/en_US/sdk.js';
          fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
      }
    
      // Here we run a very simple test of the Graph API after login is
      // successful.  See statusChangeCallback() for when this call is made.
      testAPI() {
        console.log('Welcome! Fetching your information.... ');
        FB.api('/me', function(response) {
          console.log('Successful login for: ' + response.name);
          document.getElementById('status').innerHTML =
            'Thanks for logging in, ' + response.name + '!';
        });
      }
    
      // This is called with the results from from FB.getLoginStatus().
      statusChangeCallback(response) {
        if (response.status === 'connected') {
          // Logged into your app and Facebook.
          this.testAPI();
        } else if (response.status === 'not_authorized') {
          // The person is logged into Facebook, but not your app.
          document.getElementById('status').innerHTML = 'Please log ' +
            'into this app.';
        } else {
          // The person is not logged into Facebook, so we're not sure if
          // they are logged into this app or not.
          document.getElementById('status').innerHTML = 'Please log ' +
          'into Facebook.';
        }
      }
    
      checkLoginState() {
        FB.getLoginStatus(function(response) {
          this.statusChangeCallback(response);
        }.bind(this));
      }
    
      handleClick() {
        FB.login(this.checkLoginState());
      }
    
      render() {
        return (
          

    Facebook Login

    Login
    ); } }

提交回复
热议问题