Ejemplo n.º 1
0
 /** @return \Kdyby\Facebook\Dialog\LoginDialog */
 protected function createComponentFbLogin()
 {
     $dialog = $this->facebook->createDialog('login');
     /** @var \Kdyby\Facebook\Dialog\LoginDialog $dialog */
     $dialog->onResponse[] = function (\Kdyby\Facebook\Dialog\LoginDialog $dialog) {
         $fb = $dialog->getFacebook();
         if (!$fb->getUser()) {
             $this->flashMessage("Facebook authentication failed.");
             return;
         }
         /**
          * If we get here, it means that the user was recognized
          * and we can call the Facebook API
          */
         try {
             $me = $fb->api('/me');
             if (!($existing = $this->usersModel->findByFacebookId($fb->getUser()))) {
                 /**
                  * Variable $me contains all the public information about the user
                  * including facebook id, name and email, if he allowed you to see it.
                  */
                 $existing = $this->usersModel->registerFromFacebook($fb->getUser(), $me, $this->user);
             }
             /**
              * You should save the access token to database for later usage.
              *
              * You will need it when you'll want to call Facebook API,
              * when the user is not logged in to your website,
              * with the access token in his session.
              */
             $this->usersModel->updateFacebookAccessToken($fb->getUser(), $fb->getAccessToken());
             /**
              * Nette\Security\User accepts not only textual credentials,
              * but even an identity instance!
              */
             $this->user->setExpiration('365 days', FALSE);
             $this->user->login(new \Nette\Security\Identity($existing->id, NULL, $existing));
             /**
              * You can celebrate now! The user is authenticated :)
              */
         } catch (\Kdyby\Facebook\FacebookApiException $e) {
             /**
              * You might wanna know what happened, so let's log the exception.
              *
              * Rendering entire bluescreen is kind of slow task,
              * so might wanna log only $e->getMessage(), it's up to you
              */
             \Tracy\Debugger::log($e, 'facebook');
             $this->flashMessage("Facebook authentication failed.");
         }
         $this->redirect('this');
     };
     return $dialog;
 }