webtechnick / CakePHP-Facebook-Plugin

CakePHP Facebook Plugin
http://facebook.webtechnick.com
445 stars 138 forks source link

Getting User information in beforeRender issue #145

Open robksawyer opened 11 years ago

robksawyer commented 11 years ago

I'd like to be able to get a Facebook user's information within the beforeRender method and the only way I've been able to do this is using the following method. Is there a better way?

$this->Connect->setAccessToken($this->current_user['facebook_oauth_token']); //Try to reset the oauth token
$this->Connect->uid = $this->current_user['facebook_id'];
$this->Connect->initialize($this);
$this->Connect->startup($this);
$this->facebook_user = $this->Connect->user();
amjo commented 11 years ago

to solve that i've created a Session Variable within the beforeRender of the AppController:

if($this->Auth->loggedIn()){ $id = $this->Auth->user('id'); $logged_user = $this->User->findById($id); //knowing that i've saved all the user details i've grabbed from Facebook in the users table $this->Session->write('LoggedUser', $logged_user); }

this way you can call it from any method of any controller. of course, don't forget to do "$this->Session->destroy();" upon logout.