/** * Logs in a user. * * @return mixed */ public function actionLogin() { /** @var $eauth \nodge\eauth\ServiceBase */ $eauth = Yii::$app->get('eauth')->getIdentity('steam'); $eauth->setRedirectUrl(Yii::$app->getUser()->getReturnUrl()); $eauth->setCancelUrl(Yii::$app->getUrlManager()->createAbsoluteUrl('site/login')); try { if ($eauth->authenticate()) { $identity = User::findByEAuth($eauth); $user = User::findOne(['steamid' => $identity->steamid]); if (!$user) { $user = new User(); } $user->username = $identity->username; $user->steamid = $identity->steamid; $user->profile_url = $identity->profile_url; $user->avatar = $identity->avatar; $user->avatar_md = $identity->avatar_md; $user->avatar_lg = $identity->avatar_lg; $user->generateAuthKey(); $user->save(); Yii::$app->getUser()->login($identity); $eauth->redirect(); } else { $eauth->cancel(); } } catch (ErrorException $e) { Yii::$app->getSession()->setFlash('error', 'EAuthException: ' . $e->getMessage()); $eauth->redirect($eauth->getCancelUrl()); } }
/** * Logs in a user. * * @return mixed */ public function actionLogin() { $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(); $eauth->redirect($eauth->getCancelUrl()); } } // default authorization code through login/password .. }
public function actionLogin() { if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); $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()) { $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(); $eauth->redirect($eauth->getCancelUrl()); } } elseif ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(Yii::$app->homeUrl); } else { return $this->render('login', ['model' => $model]); } }
public function actionLogin() { $this->layout = '/blog'; if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $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(); $eauth->redirect($eauth->getCancelUrl()); } } $model = new LoginForm(); if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; $model->load(Yii::$app->request->post()); return ActiveForm::validate($model); } if ($model->load(Yii::$app->request->post()) && $model->validate()) { $model->login(); return $this->goBack(); } return $this->render('login', ['model' => $model]); }
/** * @param $serviceName * @param bool $auth * @throws \yii\base\InvalidConfigException */ public static function loginByEAuth($serviceName, $auth = true) { /** @var \nodge\eauth\ServiceBase $eauth */ $eauth = Yii::$app->get('eauth')->getIdentity($serviceName); try { if ($eauth->authenticate()) { $identity = User::findByEAuth($eauth); if ($auth) { Yii::$app->getUser()->login($identity); } $eauth->redirect(); } else { $eauth->cancel(Yii::$app->getUrlManager()->createAbsoluteUrl('site/login')); } } catch (ErrorException $e) { Yii::$app->getSession()->setFlash('error', 'EAuthException: ' . $e->getMessage()); $eauth->redirect($eauth->getCancelUrl()); } }