/** * * @param \yii\authclient\ClientInterface $client * @return type */ public function successCallback($client) { // TODO: Group FK's to one local user. // Otherwise, if we log in via FB and another time via google, we // end up with two local accounts. if (!$this->action instanceof \yii\authclient\AuthAction) { throw new \yii\base\InvalidCallException("successCallback is only meant to be executed by AuthAction!"); } $attributes = $client->getUserAttributes(); $externalUser = new AuthForm(); $externalUser->authProvider = $client->getName(); $externalUser->externalUserId = array_key_exists('id', $attributes) ? $attributes['id'] : null; if ($externalUser->validate()) { Yii::info('AuthForm validated.'); if ($externalUser->isRegistered()) { Yii::info('ExternalUser is registered. Logging in and redirecting to game/index.'); $externalUser->login(); return $this->action->redirect(Url::to(['site/index'], true)); } else { throw new \yii\base\InvalidCallException("Can't login non-registered user '{$externalUser->externalUserId}@{$externalUser->authProvider}'!"); } } else { // TODO error. Throw, display actionError? Yii::info('AuthForm couldn\'t be validated. Errors: ' . print_r($externalUser->errors, true)); Yii::info('Client attributes: ' . print_r($attributes, true)); } }
public function actionLogin() { if (!Yii::$app->user->isGuest) { return $this->redirect($this->baseUrl . 'index', 302); } $model = new AuthForm(); if ($model->load(\Yii::$app->request->post())) { $identity = TAccount::findOne(['name' => $model->account, 'pwd' => md5($model->password)]); if (!empty($identity)) { Yii::$app->user->login($identity); return $this->redirect($this->baseUrl . 'index', 302); } } return $this->render('login', ['model' => $model]); }