Social Login with Facebook







Social login is a form of single sign-on using existing information from a social networking service such as Facebook, Twitter or Google+, to sign into a third party website instead of creating a new login account specifically for that website.

Social login is often implemented using the OAuth standard. OAuth is a secure authorization protocol which is commonly used in conjunction with authentication to grant 3rd party applications a "session token" allowing them to make API calls to providers on the user’s behalf. Sites using the social login in this manner typically offer social features such as commenting, sharing, reactions and gamification.





Step 1 : Create an application in the developer account on Facebook.



  • Provide a display name for your application and your contact email and create the application.


  • Once your app is created, associate “Facebook Login” with it.


  • Under the Settings of the “Facebok Login”, select the platform 'Web' for this app. And you need to provide the Redirection Endpoint URL.
  • This URL should be within your client web application and Facebook will send all responses to this URL. However, for trying out this flow, you don’t need to have a working URL available. You can simply provide a dummy URL here for the moment. The same URL you add here should be sent along with requests in next steps.


  • In the Dashboard, you can see the App ID and the App Secret for your app. In OAuth terminology, we call the same as Client ID and Client Secret, or Consumer Key and Consumer Secret.

  • Now we have successfully registered our app in facebook and configured it. You need to take down the App ID and App Secret which is generated for your app and also the Redirection Endpoint URL which you defined where we will use these three values in next steps when making requests to facebook for retrieving user resources.



Step 2 : Create a Login page (login.php).





Step 3 : Create a config.php file.


  • Start a session and call autoload.php from Facebook SDK.
  • Go to https://developers.facebook.com/ and get the client ID (app id), client secret (app secret) and API version from the application that you created.

Step 4 : In login.php set the redirect URL and Scope and get the authorization URL from the facebook.



  • Set the redirect URL to fb-callback.php file.
  • Set the permissions to 'email'.
  • Permissions are how you ask someone if you can access that data.
  • Your app has requested a person's email address but that request also automatically asks for access to a person's public profile.

Step 5 : Create the fb-callback.php and retrieve the user information from the user.



  • In here, we get the access token, to retrieve the user information.
  • If there is no access token, we redirect back to login.php.
  • With the access token, we can retrieve the user information and redirect to index.php to view them.

Step 6 : Create the index.php file to display the data retrieved using the below code.




Step 7 : Run the Application.







So this is the result.



Source code : Social Login with Facebook



Reference : https://www.youtube.com/watch?v=1DgzTWr3F2I

Comments

Post a Comment

Popular posts from this blog

Intrusion Detection System Vs. Intrusion Prevention System

Cross Site Request Forgery Prevention - Synchronizer Token Pattern