Codeignitor,Facebook javascript SDK,PHP SDK重定向在Facebook登录后不会getUser()直到刷新 [英] Codeignitor, Facebook javascript SDK, PHP SDK Redirect after facebook login doesnt getUser() until refresh

查看:134
本文介绍了Codeignitor,Facebook javascript SDK,PHP SDK重定向在Facebook登录后不会getUser()直到刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过程如下。

1. User clicks Facebook Login button
2. User signs in and grants perms
3. Facebook redirects uses FB.Event.subscribe auth.login to redirect my login controller
4. My login controller loads the fbconnect module and chains the method Connect()
5. Connect() Checks if there is a facebook user using getUser() and if true checks if they are an existing user to log them in or returns false if no facebook user was found.

一切正常,除了唯一的问题是重定向到登录页面后连接返回False,当它应该事实上,正确的是,fbuser已经登录并已经授予了perms。如果我刷新,但一切正常,用户登录,如果存在或新的发送到注册页面。以下是我的代码..

Everything works except the only problem is after being redirected to the login page Connect returns False when it should in fact be true sine the fbuser is logged in and has granted perms. If I refresh however everything works, the user is logged in if exists or if new is sent to the signup page. Below is my code..

      <div id="fb-root"></div>
    <script>
      //initializing API
      window.fbAsyncInit = function() {
        FB.init({appId: '(My app id)', status: true, cookie: true,
                 xfbml: true, // channel.html file
                 oauth  : true
                 });
                 FB.Event.subscribe('auth.login', function(response) {
                 window.location='http://www.(Mysite).com/login';
                 });
                 FB.Event.subscribe('auth.logout', function(response) {
                 window.location='http://www.(Mysite).com/logout';
                 });
                 };
      (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);
      }());
    </script>

Fbconnect.php

Fbconnect.php

class Fbconnect extends MX_Controller {
public $fb_user = null;
public $fb_user_id = null;

// Initilize Fabebook
public function __construct() 
{   
    parent::__construct();
    $this->config->load("facebook");
    require 'facebook.php';

    $this->facebook = new Facebook(array(
      'appId' => $this->config->item('appId'),
      'secret' => $this->config->item('Secret')
    ));


    // Get user from FB
    $this->fb_user_id = $this->facebook->getUser();

    if ($this->fb_user_id):
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $this->fb_user = (object)$this->facebook->api('/me');
      } catch (FacebookApiException $e) {
        error_log($e);
        $this->fb_user = null;
      }
    endif;

}


public function connect(){

    // Check for FB object
    if($this->fb_user):
        // FB Object found
            // Check if user exists
            $this->load->model('facebook/m_facebook');
            $user = $this->m_facebook->getUser($this->fb_user->id);

            // If we find a user and fb connect is true
            if($user):
                // user found log them in
                if($this->ion_auth->login($user->username, $user->password, true, true)):
                   redirect('', 'refresh');
                endif;
            else:
            // There is a fb object & the user is not logged in & doesnt exist in db yet must be new 
                redirect('signup/facebook', 'refresh');
            endif;
    else: 
        // No FB user or user not logged in                                  
        return false;
    endif;    
}

验证/登录代码(仅涉及到Facebook的部分)

The auth/login code ( just the part that involves facebook)

 $this->load->module('facebook/fbconnect')->connect(); 

提前感谢您对此问题的任何意见。

Thank you in advance for any input on this issue.

推荐答案

我似乎已经弄清楚了。我对javascript进行了更改:

Well I seem to have figured this out. I made this change to the javascript:

    FB.Event.subscribe('auth.login', function(response) {
    FB.api('/me', function(response) {
    window.location.reload();
    });
});

我想通过添加FB.api('/ me',function(response){} );它以某种方式使getUser()返回true。如果有其他人有getUser()返回0尝试这个。无论如何,我希望这有助于别人:)

I guess that by adding the FB.api('/me', function(response){}); it somehow made the getUser() return true. If anyone else is having getUser() return 0 try this. Anyway I hope this helps others :)

这篇关于Codeignitor,Facebook javascript SDK,PHP SDK重定向在Facebook登录后不会getUser()直到刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆