Facebook Login in JavaScript using Facebook SDK & Graph API

In this tutorial, I will guide you on how to add a Facebook login system to your website or app.

You might have seen many apps that accept login from different social media platforms (i.e. Facebook, Google, Microsoft, GitHub, Twitter, etc.). Basically, a social login enables us to quickly register new users in our app without forcing them to fill lengthy signup forms.

Today, we will use Facebook login JavaScript SDK and Graph API to implement Facebook login. The complete sample code of JavaScript Facebook Login with examples is given below.

Add Facebook Developer App

At first, you need to add a new app in the Facebook developers section. So, let’s add a new app and get its app ID.

Facebook developers add new app
create new app ID - Facebook developers

Now copy this app ID and store it somewhere. We will use this app ID in our application.

Facebook Login JavaScript Sample Code

After that, create a new file index.html for the login and logout button.

index.html

HTML

init.js

Here you need to copy/paste your app ID which we generated earlier at the specified location (i.e. Code Line # 13).

JavaScript

script.js

JavaScript

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.