Monday, January 7, 2013

FB-Login Auth->User() returns null after loginRedirect

Hi,

in my application there is a AccountController with loginAction.
In this loginAction there is a Facebook-Login, which is working well.
a user will be created if the facebook user isn't user at my application.

but after redirected there's no Login Session. Users getting redirected to login-page.

AppController:

App::import('Vendor', 'facebook/facebook');
class AppController extends Controller {

    public $components = array('Auth','Session');
   
    public function beforeFilter() {
        $this->Auth->loginRedirect = array('controller' => 'account', 'action' => 'dashboard');
        $this->Auth->logoutRedirect = '/';
        $this->Auth->loginAction = array('controller' => 'pages', 'action' => 'login');
    }

AccountController:

class AccountController extends AppController {

    public $uses = array('User');

    public function beforeFilter() {
        $this->Auth->allow('login');
        parent::beforeFilter();
    }
   
    public function login() {
        $this->facebook = new Facebook(array(
            'appId' => '145490xxx',
            'secret' => '95a38xxx',
            'cookie' => 'true'
        ));
       
        $user = $this->facebook->getUser();
        if($user){
            try{
                $params = array('next' => 'http://'.$_SERVER['HTTP_HOST'].'/account/logout');
                $this->Session->write('logoutLink', $this->facebook->getLogoutUrl($params));
               
                    $fb_user = $this->facebook->api('/me');
                   
                    $userData = $this->User->find('first', array(
                        'conditions' => array('fb_id' => $user),
                        'fields' => array('fb_id','password','random'),
                    ));
                    if (empty($userData)) {
                        $userData = array(
                            'fb_id' => $user,
                            'random' => $this->__randomString(),
                            'password' => $this->Auth->password($userData['random']),
                            'email' => $fb_user['email']
                           
                        );
                        $this->User->create();
                        $this->User->save($userData);
                    }
                    $this->Auth->fields = array('username' => 'fb_id', 'password' => 'random');
                    $this->Auth->login($userData);
                    $this->redirect($this->Auth->redirect());
            }catch(FacebookApiException $e){
                $user = NULL;
            }
        }
        if(empty($user)){
            $loginurl = $this->facebook->getLoginUrl(array(
                'scope'=> 'email,publish_stream,read_friendlists',
                'redirect_uri'  => 'http://'.$_SERVER['HTTP_HOST'].'/account/login',
                ));
 
            $this->redirect($loginurl);
        }
    }
   
    public function dashboard() {
        echo 'logged in';
    }


can't find any error. Pls help.

M.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: