2

ログインページにアクセスしたときにCookieが設定されているかどうかを確認し、設定されている場合はログインが自動になるようにWebサイトを設定しようとしています-つまり、このページが読み込まれると、作成する「Facebook.ashx」にリダイレクトされますアクセストークンを記憶するクッキー。

ただし、Cookie が設定されていない場合、ユーザーは [Login with Facebook] ボタンをクリックしてログインを続行する必要があります。その後、以前と同様に「Facebook.ashx」にリダイレクトされます。

これは私のコードです - 現時点では、ユーザーがアプリを受け入れて「Facebook.ashx」に進むと、常にログインします

<div id="fb-root"></div>  
<script type="text/javascript">
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'My_App_ID', // App ID
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });

        // Additional initialization code here

        FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.status === 'connected') {
                // the user is logged in and has authenticated your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                // TODO: Handle the access token
                // Do a post to the server to finish the logon
                // This is a form post since we don't want to use AJAX
                var form = document.createElement("form");
                var redirect = "FacebookLogin.ashx?redirect=" + document.getElementById('redirect').innerHTML;
                form.setAttribute("method", 'post');
                form.setAttribute("action", redirect);

                var field = document.createElement("input");
                field.setAttribute("type", "hidden");
                field.setAttribute("name", 'accessToken');
                field.setAttribute("value", accessToken);
                form.appendChild(field);

                document.body.appendChild(form);
                form.submit();

            } else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                // but has not authenticated your app
            } else {
                // the user isn't logged in to Facebook.
            }
        });
    };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));
</script>

<div class="fb-login-button" scope="email, publish_stream">Login with Facebook</div>

事前に助けてくれてありがとう

4

2 に答える 2

1

auth.authResponseChangeユーザーがサインインしている限り、ページが読み込まれるたびに発生します。

あなたがしたいことは、「既知の状態」をサーバーからクライアントに渡し、これを内部で比較することFB.getLoginStatusです。状態が変更された場合 (userID など)、これは実際のログインを構成し、ashx ページにリダイレクトして新しい Cookie を設定します。

簡単な例として:

  • ステップ 1 - ページのロード、空の状態のフラッシュ (userID = 0)
  • ステップ 2 - FB.getLoginStatus を実行し、authResponse.userID を userID と比較します
  • ステップ 3 - authResponse.userID <> 0 であるため、Cookie を設定するページにリダイレクトします
  • ステップ 4 - ページの読み込み、状態のフラッシュ (userID = authResponse.userID からの userID)
  • ステップ 5 - FB.getLoginStatus を実行し、authResponse.userID と userID を比較します。一致するため何もしません。
于 2012-09-05T05:25:09.117 に答える
1

私はそれを解決しました-簡単な修正

ステータスを true ではなく false に変更すると、ユーザーがログインをクリックして Cookie が存在するかどうかを確認するのを待ちます。単純な if ステートメントを使用して、ステータスを true または false に変更します。

if (document.cookie.indexOf("fb_token") > 0) //user has already logged in with facebook - process should be automatic
        var fb_status = true;
    else
        var fb_status = false;
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'My App ID', // App ID
            status: fb_status, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });
于 2012-09-05T20:02:12.927 に答える