如何正确处理用户注册和重定向?

时间:2012-05-31 21:30:16

标签: facebook facebook-javascript-sdk registration

我有一个使用旧版FB API的注册表单。

我正在尝试将其更新为当前版本,并且在处理来自FB的响应时遇到问题。

点击FB登录按钮并授予权限(email,publish_stream)后,我找不到方法知道用户已授予访问权限(而不仅仅是以已注册的用户身份登录),并且需要进入下一页登记表格。

成功的权限请求后的响应与成功登录相同:

{
    authResponse:
    {
        accessToken:    AAAFEIew...URYo,
        userID:         100003908595992,
        expiresIn:      6014,
        signedRequest:  uhTqZodDn0QUoWAUBuYEKmlaM8zViO0r_fnKONaC4v8.eyJhbGdvcml...k5MiJ9,
    },
    status: connected
}

如何判断登录是否是已注册用户或新注册的结果?

我正在使用javascript SDK。

代码:

<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>


                <div id="fb-root" style="padding: 0px;"></div>
                <script>
                    window.fbAsyncInit = function() {
                        FB.init({
                            appId      : 'API_KEY',
                            status     : true,
                            channelUrl : '//'+window.location.hostname+'/channel.php',
                            cookie     : true,
                            xfbml      : true,
                            oauth      : true
                        });

                        // listen for and handle auth.statusChange events
                        FB.Event.subscribe('auth.statusChange', function(response) {
                            if (response.authResponse) {
                                // user has auth'd your app and is logged into Facebook
                                alert('logged in!');
                            }
                            else
                            {
                                // user has not auth'd your app, or is not logged into Facebook
                                alert('not logged in!');
                            }
                        });

                    };
                    (function(d){
                        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                        js = d.createElement('script'); js.id = id; js.async = true;
                        js.src = "//connect.facebook.net/en_US/all.js";
                        d.getElementsByTagName('head')[0].appendChild(js);
                    }(document));

2 个答案:

答案 0 :(得分:0)

这是我在尝试做你正在做的事情时使用的资源。它运作得非常好。

window.fbAsyncInit = function() {
    FB.init({ appId: 'Your_APP_ID',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true});

    showLoader(true);

    function updateButton(response) {
        button       =   document.getElementById('fb-auth');
        userInfo     =   document.getElementById('user-info');

        if (response.authResponse) {
            //user is already logged in and connected
            FB.api('/me', function(info) {
                login(response, info);
            });

            button.onclick = function() {
                FB.logout(function(response) {
                    logout(response);
                });
            };
        } else {
            //user is not connected to your app or logged out
            button.innerHTML = 'Login';
            button.onclick = function() {
                showLoader(true);
                FB.login(function(response) {
                    if (response.authResponse) {
                        FB.api('/me', function(info) {
                            login(response, info);
                        });
                    } else {
                        //user cancelled login or did not grant authorization
                        showLoader(false);
                    }
                }, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'});
            }
        }
    }

    // run once with current status and whenever the status changes
    FB.getLoginStatus(updateButton);
    FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol
        + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}());

参考:http://thinkdiff.net/facebook/new-javascript-sdk-oauth-2-0-based-fbconnect-tutorial/

信仰斯隆

答案 1 :(得分:0)

我意识到我需要在FB SDK中使用getLoginStatus函数。

我的功能代码是:

<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId       : '<?php echo HSWI_FB_KEY; ?>',
            status      : true,
            logging     : true,
            channelUrl  : '//'+window.location.hostname+'/channel.php',
            cookie      : true,
            xfbml       : true,
            oauth       : true
        });

        FB.Event.subscribe('auth.login', function(response)
        {
            window.location.href    = '/login_script.php';
        });

        FB.Event.subscribe('auth.logout', function(response) {
            window.location.reload();
        });

        checkLoggedInFB();

    };
    (function(d){
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    }(document));

    function fbLogin()
    {
        FB.login(
            function(response)
            {
                if(response.authResponse)
                {
                    window.location.href    = '/login_script.php';
                }
            }
        );
    }

    function checkLoggedInFB()
    {
        try
        {
            FB.getLoginStatus(
                function(response)
                {
                    if(response.status == 'connected')
                    {
                        window.location.href    = '/login_script.php';
                    }
                }
            );
        }
        catch(e)
        {
            alert('Error checking if logged into FB: '+ e.message);
            return false;
        }
        return true;
    }

</script>
<style type="text/css">
    .fb-login-button
    {
        margin-top: -15px;
    }

    .fb-login-button.fb_iframe_widget.fb_hide_iframes
    {
        display: none;
    }
</style>
<div class="fb-login-button" scope="email,publish_stream,user_birthday,user_location,user_about_me">Login</div>