예제 #1
0
 /**
  * Logs in a user.
  *
  * @return mixed
  */
 public function actionLogin()
 {
     if (!\Yii::$app->user->isGuest) {
         return $this->redirect(['user-panel/settings']);
     }
     $serviceName = Yii::$app->getRequest()->getQueryParam('service');
     if (isset($serviceName)) {
         /** @var $eauth \nodge\eauth\ServiceBase */
         $eauth = Yii::$app->get('eauth')->getIdentity($serviceName);
         $eauth->setRedirectUrl(Yii::$app->getUser()->getReturnUrl());
         $eauth->setCancelUrl(Yii::$app->getUrlManager()->createAbsoluteUrl('site/login'));
         try {
             if ($eauth->authenticate()) {
                 // var_dump($eauth->getIsAuthenticated(), $eauth->getAttributes()); exit;
                 $identity = User::findByEAuth($eauth);
                 Yii::$app->getUser()->login($identity);
                 // special redirect with closing popup window
                 $eauth->redirect();
             } else {
                 // close popup window and redirect to cancelUrl
                 $eauth->cancel();
             }
         } catch (\nodge\eauth\ErrorException $e) {
             // save error to show it later
             Yii::$app->getSession()->setFlash('error', 'EAuthException: ' . $e->getMessage());
             // close popup window and redirect to cancelUrl
             // $eauth->cancel();
             $this->redirect($eauth->getCancelUrl());
         }
     } else {
         $model = new LoginForm();
         if ($model->load(Yii::$app->request->post()) && $model->login()) {
             return $this->goBack();
         } else {
             return $this->render('login', ['model' => $model]);
         }
     }
 }