Proces jest następujący.Codeignitor, Facebook javascript SDK, PHP SDK Przekierowanie po zalogowaniu facebook nie robi getUser() aż do odświeżenia
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.
Wszystko działa z wyjątkiem Jedynym problemem jest po przekierowany na stronę logowania Połącz False gdy powinna ona być w rzeczywistości prawdziwy sinus fbuser jest zalogowany i przyznało perms. Jeśli odświeżam, ale wszystko działa, użytkownik jest zalogowany, jeśli istnieje lub jest wysyłany na stronę rejestracji. Poniżej jest mój kod ..
<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
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;
}
kod auth/login (tylko ta część, która polega na facebook)
$this->load->module('facebook/fbconnect')->connect();
Z góry dziękuję za wszelkie wejścia w tej sprawie.
Z jakiej przeglądarki korzystasz? FB naprawia problemy, w których plik cookie nie jest czytany przy pierwszym pobraniu z SDK (musisz odświeżyć, aby działał). – ifaour