I have PHP-Class to work with Facebook API:
class Social {
private $name;
private $context;
private $tocken;
public function check(){
$this->tocken = $this->context->getAccessToken(); // get current tocken
if (isset($_SESSION['fb_tocken']) AND !empty($_SESSION['fb_tocken'])){
// session tocken not null
if ($_SESSION['fb_tocken'] == $this->tocken){
// session tocken = current tocken
// update last active
return true;
} elseif ($this->tocken == APPLICATION_TOCKEN) {
// current tocken == default tocken
$this->logout();
} else {
// current tocken <> default tocken
$this->logout();
}
} else {
// session tocken is null
if ($this->tocken == APPLICATION_TOCKEN){
// current tocken is default
return false;
} else {
// current tocken is not default
if ($this->getUserInfo()){
// user registered - login action
if ($this->login())
return true;
else
return false;
} else {
// register new user
if ($this->register()){
if ($this->login()){
return true;
} else
return false;
} else
return false;
}
}
}
}
// ...
};
If user used alogritm:
1 – Login in facebook
2 – Login in application
3 – Logout from facebook
4 – Login in facebook
5 – Open application
Application Problem:
after fifth step my app can’t authorize a user. But if user press ‘refresh’ he become authorized.
That is the correct behviour – but impossible to confirm without the rest of your code (i.e. the bit that grabs the token).
What happens is that when a user logs out, the current token (that you have stored in your session) becomes invalid. The user logs back into Facebook, you need to get the new token, and that’s not going to happen automtically – but when you do a refresh, I presume your code getAccessToken() (not shown above) will grab the new token.
The simplest fix is to add a listener in Javascript for logging in and out. You can find the details here and you need the auth.login handler. When you have that triggered, simply refresh the page.
There are other options using Javascript tot pump the new token to a PHP listener that can update your session behind the scenes.