{"id":3008,"date":"2022-09-07T23:41:03","date_gmt":"2022-09-07T18:41:03","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=3008"},"modified":"2022-09-07T23:41:05","modified_gmt":"2022-09-07T18:41:05","slug":"facebook-login-in-javascript-using-facebook-sdk-graph-api","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/","title":{"rendered":"Facebook Login in JavaScript using Facebook SDK &#038; Graph API"},"content":{"rendered":"\n<p>In this tutorial, I will guide you on how to add a Facebook login system to your website or app.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Today, we will use <strong>Facebook login JavaScript SDK<\/strong> and <strong>Graph API<\/strong> to implement Facebook login. The complete sample code of JavaScript Facebook Login with examples is given below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add Facebook Developer App<\/h2>\n\n\n\n<p>At first, you need to add a new app in the <a href=\"https:\/\/developers.facebook.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Facebook developers<\/a> section. So, let&#8217;s add a new app and get its app ID.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"198\" height=\"234\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/scrnli_9_7_2022_11-09-34-PM.png\" alt=\"Facebook developers add new app\" class=\"wp-image-3009\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"899\" height=\"385\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/scrnli_9_7_2022_11-16-18-PM.png\" alt=\"create new app ID - Facebook developers\" class=\"wp-image-3011\" srcset=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/scrnli_9_7_2022_11-16-18-PM.png 899w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/scrnli_9_7_2022_11-16-18-PM-300x128.png 300w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/scrnli_9_7_2022_11-16-18-PM-768x329.png 768w\" sizes=\"auto, (max-width: 899px) 100vw, 899px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"245\" height=\"120\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/download-43.png\" alt=\"\" class=\"wp-image-3012\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"119\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/img14.png\" alt=\"\" class=\"wp-image-3013\" srcset=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/img14.png 363w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/img14-300x98.png 300w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/figure>\n\n\n\n<p>Now copy this app ID and store it somewhere. We will use this app ID in our application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Facebook Login JavaScript Sample Code<\/h2>\n\n\n\n<p>After that, create a new file index.html for the login and logout button.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">index.html<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Facebook Login using JavaScript and jQuery&lt;\/title&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;\/head&gt;\n&lt;style&gt;\n&lt;\/style&gt;\n&lt;script src=&quot;init.js&quot;&gt;&lt;\/script&gt;\n&lt;body&gt;\n    &lt;button id=&quot;login&quot;&gt;Login with Facebook&lt;\/button&gt;\n    &lt;button style=&quot;display:none;&quot; id=&quot;logout&quot;&gt;Logout&lt;\/button&gt;           \n    &lt;div id=&quot;status&quot;&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;script src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.3.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script src=&quot;script.js&quot;&gt;&lt;\/script&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">init.js<\/h2>\n\n\n\n<p>Here you need to copy\/paste your app ID which we generated earlier at the specified location (i.e. Code Line # 13).<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">(function(d, s, id) {\n    var js, fjs = d.getElementsByTagName(s)[0];\n    if (d.getElementById(id)) return;\n    js = d.createElement(s); js.id = id;\n    js.src = &quot;https:\/\/connect.facebook.net\/en_US\/sdk.js&quot;;\n    fjs.parentNode.insertBefore(js, fjs);\n  }(document, 'script', 'facebook-jssdk'));\n \n  \/\/ initialize the facebook sdk\n \n  window.fbAsyncInit = function() {\n    FB.init({\n      appId      : '#### YOUR APP ID ########',\n      cookie     : true,  \/\/ enable cookies to allow the server to access \n                          \/\/ the session\n      xfbml      : true,  \/\/ parse social plugins on this page\n      version    : 'v3.1' \/\/ The Graph API version to use for the call\n    });\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">script.js<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">$(document).ready(function(){   \n \n  \/\/ add event listener on the login button\n  $(&quot;#login&quot;).click(function(){\n    \n    facebookLogin();\n    \n  });\n  \n  \/\/ add event listener on the logout button\n  $(&quot;#logout&quot;).click(function(){\n \n    $(&quot;#logout&quot;).hide();\n    $(&quot;#login&quot;).show();\n    $(&quot;#status&quot;).empty();\n    facebookLogout();\n \n  });\n  \n  function facebookLogin()\n  {\n    FB.getLoginStatus(function(response) {\n        console.log(response);\n        statusChangeCallback(response);\n    });\n  }\n  \n  function statusChangeCallback(response)\n  {\n      console.log(response);\n      if(response.status === &quot;connected&quot;)\n      {\n         $(&quot;#login&quot;).hide();\n         $(&quot;#logout&quot;).show(); \n         fetchUserProfile();\n      }\n      else{\n          \/\/ Logging the user to Facebook by a Dialog Window\n          facebookLoginByDialog();\n      }\n  }\n  \n  function facebookLoginByDialog()\n  {\n    FB.login(function(response) {\n       \n        statusChangeCallback(response);\n       \n    }, {scope: 'public_profile,email'});\n  }\n  \n  function fetchUserProfile()\n  {\n    console.log('Welcome!  Fetching your information.... ');\n    FB.api('\/me?fields=id,name,email,gender,birthday', function(response) {\n      console.log(response);\n      console.log('Successful login for: ' + response.name);\n      var profile = `&lt;h1&gt;Welcome {response.name}&lt;h1&gt;\n      &lt;h2&gt;Your email is ${response.email}&lt;\/h2&gt;\n      &lt;h3&gt;Your Birthday is ${response.birthday}&lt;\/h3&gt;`;\n      $(&quot;#status&quot;).append(profile);\n    });\n  }\n  \n  function facebookLogout()\n  {\n    FB.logout(function(response) {\n        statusChangeCallback(response);\n    });\n  }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <a title=\"Facebook Login in JavaScript using Facebook SDK &#038; Graph API\" class=\"read-more\" href=\"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/\" aria-label=\"Read more about Facebook Login in JavaScript using Facebook SDK &#038; Graph API\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-3008","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Facebook Login in JavaScript using Facebook SDK &amp; Graph API<\/title>\n<meta name=\"description\" content=\"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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Facebook Login in JavaScript using Facebook SDK &amp; Graph API\" \/>\n<meta property=\"og:description\" content=\"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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-07T18:41:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-07T18:41:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/scrnli_9_7_2022_11-09-34-PM.png\" \/>\n<meta name=\"author\" content=\"Furqan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Furqan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Facebook Login in JavaScript using Facebook SDK & Graph API","description":"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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/","og_locale":"en_US","og_type":"article","og_title":"Facebook Login in JavaScript using Facebook SDK & Graph API","og_description":"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","og_url":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-09-07T18:41:03+00:00","article_modified_time":"2022-09-07T18:41:05+00:00","og_image":[{"url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/scrnli_9_7_2022_11-09-34-PM.png","type":"","width":"","height":""}],"author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"Facebook Login in JavaScript using Facebook SDK &#038; Graph API","datePublished":"2022-09-07T18:41:03+00:00","dateModified":"2022-09-07T18:41:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/"},"wordCount":194,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/","url":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/","name":"Facebook Login in JavaScript using Facebook SDK & Graph API","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","datePublished":"2022-09-07T18:41:03+00:00","dateModified":"2022-09-07T18:41:05+00:00","description":"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","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","width":880,"height":495,"caption":"Default Featured Image"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/facebook-login-in-javascript-using-facebook-sdk-graph-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Facebook Login in JavaScript using Facebook SDK &#038; Graph API"}]},{"@type":"WebSite","@id":"https:\/\/www.edopedia.com\/blog\/#website","url":"https:\/\/www.edopedia.com\/blog\/","name":"Edopedia","description":"Coding\/Programming Blog","publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edopedia.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.edopedia.com\/blog\/#organization","name":"Edopedia","url":"https:\/\/www.edopedia.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","width":400,"height":100,"caption":"Edopedia"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339","name":"Furqan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","caption":"Furqan"},"description":"Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.","sameAs":["http:\/\/www.edopedia.com\/blog\/","trulyfurqan"],"url":"https:\/\/www.edopedia.com\/blog\/author\/furqan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/comments?post=3008"}],"version-history":[{"count":3,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3008\/revisions"}],"predecessor-version":[{"id":3015,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3008\/revisions\/3015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/1762"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=3008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=3008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=3008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}