Search code examples
phpmysqlfacebookregistration

I want to have a "Sign Up with Facebook" feature for my php application


We are creating a PHP MySQL application in which we have a registration form for the users.

We want "Sign Up with Facebook" functionality as well for the registration.

I have googled about it but I could only find the code for the Login functionality for Facebook. If anyone could help me to find the exact resource to know about the Registration through Facebook feature, it would be really great.

Thanks in advance.


Solution

  • To piggyback on CBroe's answer, Facebook only has login however, you can use the continue with button as a psuedo "registration" button. Here's where to find/configure the continue with button: https://developers.facebook.com/docs/facebook-login/web/login-button/ https://developers.facebook.com/docs/facebook-login/web

    With the results from Facebook based on their docs, get the variables you need to register a user in your system. Here's what I am doing to combat the same issue. I have a function called getUserInfo() below:

    function getUserInfo() {
    
        // Get what you need from FB's graphQL API
        FB.api('/me?fields=id,email,first_name,last_name', function (response, global) {
    
            // Set variables based on results of '/me' Graphql call
            let fbuserid = response.id;
            let fbuserfname = response.first_name;
            let fbuserlname = response.last_name;
            let fbuseremail = response.email;
    
            // I am defining variables for my site's registration form fields
            let fname = document.getElementById('fname');
            let lname = document.getElementById('lname');
            let email = document.getElementById('email');
    
            // Set input values in your form from the facebook results
            fname.value = fbuserfname;
            lname.value = fbuserlname;
            email.value = fbuseremail;
    
            // I make up a password but you don't have to
            var md = forge.md.sha256.create();
            md.update(fbuserid + fbuseremail + randomString());
            document.getElementById('pwrd').value = md.digest().toHex();
    
            // My form has steps so I auto-progress users to the next step since the Javascript filled in the form already. Completely optional
            document.getElementById("nextbutton").click();
    
        });
    }
    

    I call the getUserInfo() function in the FB button after the login event like this:

    <div id="fbregister" data-size="medium" data-button-type="continue_with" data-layout="default" data-auto-logout-link="false" data-use-continue-as="false" data-scope="email" data-onlogin="getUserInfo()"></div>
    

    What happens overall is, users click, continue with facebook, they login, the function is called, fields are set via JS and users progress to the next step. Once they finish the next steps, they are registered. I hope this helps got you off to the right start.