Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:54:38+00:00 2026-05-30T05:54:38+00:00

really need your help with log-in-with-facebook feature I’m trying to implement on my website.

  • 0

really need your help with log-in-with-facebook feature I’m trying to implement on my website.

Basically, I’m trying to achieve the following:

  • if user has acknowledged the app before and clicks FB Log in button
    on my website, they get logged into the website (with website’s user
    account associated with the Facebook user ID)

  • if user has not acknowledged the app before, on FB log in (and subscription to app) they get redirected to website’s registration page. Here the form is pre-filled with user data
    coming through Facebook and registration process becomes easier and faster.

I’m using the code below.

<body>

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
          appId: 'xxxxxxxx',
          status: true,
          cookie: true, 
          xfbml: true,
          oauth: true
        });

        FB.Event.subscribe('auth.login', function(response) {
              if (response.status === 'connected') {
                    var uid = response.authResponse.userID;
                    var accessToken = response.authResponse.accessToken;

                    if ((parseFloat(uid) == parseInt(uid)) && !isNaN(uid)) {
                        $.ajax({
                            url: '/user_actions/prep_facebook_registration',
                            cache: false,
                            type: 'POST',
                            data: { 'uid': uid, 'token': accessToken },
                            dataType: 'json',
                            success: function(data) {
                                if (data.success=='true') {
                                    if ((typeof(data.redirect) != 'undefined')) {
                                        window.location=data.redirect;
                                    }
                                }
                            }
                        });
                    }
              }
        });

        FB.Event.subscribe('auth.logout', function(response) {
            FB.getLoginStatus(function(res) {
                if (!res.authResponse) {
                    window.location='/access/logout';
                }
            });
        });

        FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            //not doing anything so far
          }
        });
      };
            (function(d, s, id) {
          var js, fjs = d.getElementsByTagName(s)[0];
          if (d.getElementById(id)) {return;}
          js = d.createElement(s); js.id = id;
          js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
          fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
</script>

LogIn button lays further down in a website:

<div class="fb-login-button" autologoutlink="true" data-show-faces="false" data-width="166" data-max-rows="1" style="position: absolute; top: 5px; right: 0;" scope="email"></div>

The problem here is that FB.Event.subscribe(‘auth.login’) triggers not only on FB log in button click, but also if user has logged to Facebook and only after comes to my website. This causing a redirection to registration page or current page reload even if user hasn’t clicked the login button!

‘/user_actions/prep_facebook_registration’ holds a script, which is checking if the member has to be redirected to registration page or should be loggedin with local account. It returns the URL to redirect.

What am I doing wrong? How can I avoid FB.Event.subscribe(‘auth.login’) being triggered outside the login button click?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:54:40+00:00Added an answer on May 30, 2026 at 5:54 am

    =====================

    Edit: Ok, I think there’s a bit of a progress here 🙂

    I’ve extended the FB login button with onlogin event which calls a JS function Facebook_login()

    <div class="fb-login-button" onlogin="Facebook_login()" autologoutlink="true" [parameters]></div>
    

    I’ve exported uid and accessToken variables outside window.fbAsyncInit function so I could access them from anywhere else:

    var uid;
    var accessToken;
    
    window.fbAsyncInit = function() {
       FB.init({
          your parameters
       });
    });
    

    Then, I got FB.Event.subscribe(‘auth.login’) to only assign values to my uid and accessToken variables, but not perform any redirection.

    FB.Event.subscribe('auth.login', function(response) {
      if (response.status === 'connected') {
        uid = response.authResponse.userID;
        accessToken = response.authResponse.accessToken;
      }
    });
    

    And finally, my custom function Facebook_login () fetches the uid and accessToken values and does the rest of the operation. This time it happens only when login button is clicked. Please note, this function is outside window.fbAsyncInit()

    function Facebook_login () {
              if ((parseFloat(uid) == parseInt(uid)) && !isNaN(uid)) {
                    $.ajax({
                        url: '/user_actions/prep_facebook_registration',
                        cache: false,
                        type: 'POST',
                        data: { 'uid': uid, 'token': accessToken },
                        dataType: 'json',
                        success: function(data) {
                            if (data.success=='true') {
                                if ((typeof(data.redirect) != 'undefined')) {
                                    if (data.redirect=='current') {
                                        location.reload();
                                    } else {
                                        window.location=data.redirect;
                                    }
                                }
                            }
                        }
                    });
                }
          }
    

    This seems to do a job for me. If anyone has any better solutions, your input would be appreciated 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i really need your help. I'm trying to get request token from twitter with
I really need your help in my issue very quickly and it's too close
I really need your help for this. I am relatively new to programming and
I've some questions .. and I really need your help. I have an application.
i really need your help with a CSS-Layout. I tried a few time, however
i really really need your help. I'm successfully setting up a connection to my
Afternoon all, I really need your help as this is a Nightmare! I was
hy guys, i really need your help. i've succesfully connected to ftp server via
I apologize for a lengthy question, but I really need your help. As a
I really need your help. I have a database with latitude and longitude data,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.