Implement Facebook API login with reactjs

后端 未结 10 1983
广开言路
广开言路 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 10:08

    ritmatter gave a good answer, but I'll show how I did this a little differently. I wanted to use Facebook's login button rather than my own to trigger the callback for checking login state. The login button might look like this:

    
    

    The button has an onlogin attribute, which the jsx parser doesn't support. Using data-onlogin in the react fashion wasn't working, so I just added the button in componentDidMount:

            componentWillMount: function () {
                    window['statusChangeCallback'] = this.statusChangeCallback;
                    window['checkLoginState'] = this.checkLoginState;
            },
            componentDidMount: function () {
                var s = '';
    
                var div = document.getElementById('social-login-button-facebook')
                div.innerHTML = s;
            },
            componentWillUnmount: function () {
                delete window['statusChangeCallback'];
                delete window['checkLoginState'];
            },
            statusChangeCallback: function(response) {
               console.log(response);
            },
            // Callback for Facebook login button
            checkLoginState: function() {
                console.log('checking login state...');
                FB.getLoginStatus(function(response) {
                   statusChangeCallback(response);
                });
            },
            render: function() {
    
                return (
                    
    ); }

    All that's left to do is trigger the button upon mounting the component if you want to automatically call checkLoginState.

提交回复
热议问题