Closed vigorouscoding closed 11 years ago
It seems the access_token is not present during the first page load.
echo FB::getAccessToken();outputs this for every user I tested it with so far. (In case this is an access token of sorts I edited it slightly for anonymization)
182512728464031|f08c6880f70c39474b1a2a39aad7974f
Notice the "|" in there?
Reloading the page outputs a real access token.
Seems like I figured it out - I was reading the userinfo in the beforeFilter of AppController. After moving this to the UsersController this behavior stopped occurring...
Hope this may help someone else who is facing the same problem.
in AppController
function beforeFilter(){
$this->set("facebookuser", $this->Connect->user());
}
and home.ctp containing
if ($facebookuser){
// do logged in stuff
echo ($this->Facebook->logout());
}else{
echo $this->Facebook->login(array(
'perms' => 'manage_pages,publish_stream'
));
}
doesn't work anymore,
how did you fix that?
I'm not using this anymore, but IIRC I just moved the stuff I had in AppControllers beforeFilter to the specific controller I needed it in.
I found that the best way to deal with first-time comers along with a way to get updated on the user information (/me) is that : In AppController.php :
public function beforeFilter(){
if($this->Connect->FB->getUser() != 0){
$this->set('facebookUser', $this->Connect->user());
}
This way, first $this->Connect->FB->getUser() initialize the component, fills in FB->uid, etc. That way you get a 0 if user is not logged in Facebook OR as not granted access to your app. Both situations, you don't want to call ->api(/me) (throught Connect->user() ) and instead redirect your user to a welcome page or directly to the login dialog.
Note, that the important function to work with is FB->getUser(), which parse the ['signed_request'], etc. Connect->user() essentially just fill in the Session['FB'] array. For you to decide if this data is useful.
On several WorkController.php, I only use
public function beforeFilter(){
$this->Connect->FB->getUser();
}
Connect component is initialized with data, from which Connect will retrieve Auth user and fill in Session['Auth']['User']['xxdetails'] . If you've got no need for specific Facebook details, and just need to deal with your database User[id], keep it simple ;)
EDIT: I'm using the current versions of CakePHP (2.3.1) and cloned this repo yesterday (March, 20th 2013).
FB::api('/me') does not work on FIRST page load - refreshing the page helps. I can't seem to put a finger on the why.
In FB.php the function "__callstatic" logs an error, but I don't know how to fix it. I tried redirecting once, so it would maybe have that token, but to no avail.
Anyone else facing the same issue ?