Exemple #1
0
 protected function createComponentFbLogin()
 {
     $dialog = $this->facebook->createDialog('login');
     $dialog->setScope(['public_profile', 'email']);
     /** @var \Kdyby\Facebook\Dialog\LoginDialog $dialog */
     $dialog->onResponse[] = function (\Kdyby\Facebook\Dialog\LoginDialog $dialog) {
         $fb = $dialog->getFacebook();
         if (!$fb->getUser()) {
             $this->flashMessage("Ověření proti Facebooku se nepodařilo. Nebyly vráceny žádné informace o uživateli.", "danger");
             return;
         }
         try {
             $me = $fb->api('/me');
             $data = $fb->getUser();
             if (!($data = $this->facebookUserModel->exists($fb->getUser()))) {
                 $this->redirect('facebook');
             } else {
                 $this->facebookUserModel->authenticate(array($fb->getUser()));
                 $this->facebookUserModel->updateToken($fb->getUser(), $fb->getAccessToken());
                 $this->flashMessage("Uživatel byl přihlášen prostřednictvím Facebooku.", "success");
             }
         } catch (\Kdyby\Facebook\FacebookApiException $e) {
             \Tracy\Debugger::log($e, 'facebook');
             $this->flashMessage("Během ověřování přes Facebook došlo k chybě.", "danger");
         }
         $this->restoreRequest($this->backlink);
         $this->redirect(':Front:Homepage:');
     };
     return $dialog;
 }
Exemple #2
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;
 }
Exemple #3
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->findUserByFacebookId($fb->getUser()))) {
                 throw new \Exception('Uživatel není aktivní nebo neexistuje.');
                 /**
                  * 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);
             }
             /**
              * 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->updateFacebookAccessToken($fb->getUser(), $fb->getAccessToken());
             $this->user->setExpiration('14 days', FALSE);
             /**
              * Nette\Security\User accepts not only textual credentials,
              * but even an identity instance!
              */
             $this->user->login(new \Nette\Security\Identity($existing->id, explode(';', $existing->role), $existing));
             $this->flashMessage('Byl/a jste úspěšně přihlášen/a prostřednictvím Facebooku jako "' . $existing->username . '"', 'success');
             $this->redirect('Homepage:');
         } 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 hard.");
         } catch (\Nette\Application\AbortException $e) {
             throw $e;
         } catch (\Exception $e) {
             $this->flashMessage("Při přihlašování došlo k chybě.", "danger");
         }
         $this->redirect('this');
     };
     return $dialog;
 }
Exemple #4
0
 /**
  * @return \Kdyby\Facebook\Dialog\LoginDialog
  */
 protected function createComponentFbLogin()
 {
     $dialog = $this->facebookClient->createDialog('login');
     /** @var LoginDialog $dialog */
     $dialog->onResponse[] = function (LoginDialog $dialog) {
         $fb = $dialog->getFacebook();
         try {
             $me = $fb->api('/me?fields=name,first_name,last_name,email');
             try {
                 $this->user->login("facebook", array("email" => $me->email, "firstName" => $me->first_name, "lastName" => $me->last_name));
                 $this->notify("Uživatel se úspěšně přihlášil.");
                 $this->getPresenter(true)->redirect("Homepage:");
             } catch (AuthenticationException $e) {
                 $this->notify("Uživateli se nepovedlo přihlásit přes službu Facebook. " . $e->getMessage());
                 $this->flashMessage($e->getMessage());
                 $this->getPresenter(true)->redirect("Homepage:");
             }
         } catch (FacebookApiException $e) {
             $this['form']->addError("Uživateli se nepovedlo přihlásit přes službu Facebook. " . $e->getMessage());
         }
     };
     return $dialog;
 }
 /** @return FacebookLoginDialog */
 protected function createComponentFacebookLogin()
 {
     /** @var FacebookLoginDialog $dialog */
     $dialog = $this->facebook->createDialog('login');
     $presenter = $this;
     $dialog->onResponse[] = function (FacebookLoginDialog $dialog) use($presenter) {
         $fb = $dialog->getFacebook();
         if (!$fb->getUser()) {
             $presenter->flashMessageLoginFailed('Facebook');
             return;
         }
         try {
             $facebookMe = $fb->api('/me');
             $facebookUserId = $fb->getUser();
             $presenter->getUser()->login($presenter->usersFacade->authenticateUserFromFacebook($facebookUserId, $facebookMe));
             $presenter->flashMessageLoginSuccess('Facebook');
         } catch (FacebookApiException $e) {
             $presenter->flashMessageLoginFailed('Facebook');
             $presenter->redirect('login');
         }
         $presenter->finalRedirect();
     };
     return $dialog;
 }