Friday, June 1, 2012

How to let Facebook Login button redirect to a particular URL

This is the info on Facebook Login button

So it will render a Login button, and a user can click on it to log in on Facebook (a log in window will pop up) But after the user logs in, even though the Like or Share buttons work now, but the Log in button still shows.

1) Is there a way to redirect to a URL after the user successfully logs in?

2) Another way is to dynamically change the Log in button to invisible or better yet, show it as "Logged in as [Peter (username)]"

How can (1) and/or (2) be done? (I don't see a callback URL in the Facebook app setting and also the redirection may need to go to different URL from page A or page B on the website)

Update: I found some info about <fb:login-button on-login="top.location = '...'; "> but I see some website doing the redirect but there is no on-login='...'

Source: Tips4all


  1. 1) You can also redirect on login using some code like this (note the auth.login event):

    <script src="">
    appId: '??????????????', cookie: true,
    status: true, xfbml: true
    FB.Event.subscribe('auth.login', function () {
    window.location = "";
    Login with Facebook

    2) To determine whether to show or hide the login button, you can use FB.getLoginStatus to discover whether the user is logged in. The following page might be of use:

  2. The website URL you redirect to ultimately needs to be on the same domain as you've configured for your app in the app settings. Basically this means you need to update your Facebook page or App Page with the correct URL. As long as you have this URL the facebook login will function properly on redirect.

  3. FBML option: This should give you the functionality you want (with xfbml:true in FB.init):

    <fb:login-button autologoutlink="true" onlogin="OnRequestPermission();">

    When the user is logged in, it changes to "Log Out." Also, if the user has not granted permissions to the app, it pops-up the request permission dialog.

    Custom option: If you don't like using FBML, you can make your own Facebook login button like this:


    <button id="THE_BUTTON">Login</button>


    FB.init({appId: 'YourAPPID', status: true, cookie: true, xfbml: true, oauth : true});
    FB.getLoginStatus(function(response) {
    if (response.status.toString().indexOf("connected")>-1) {
    initAll(); //User is connected and granted permissions
    FB.api("/me", function (response) {
    document.getElementbyId("THE_BUTTON").value =
    "Logged in as " +;
    } else {
    // This URL is specially formed to ask permissions for your app. You change the
    // permissions and your appID
    //redirect_uri = Change this to your desired callback URL

    So if the user is logged in, the button will be replaced with "Logged in as user's name." Otherwise, the OAuth Dialog will appear.