protected function attemptLogin($client)
{
/** @var \amnah\yii2\user\models\User $user */
/** @var \amnah\yii2\user\models\UserAuth $userAuth */
/** @var \amnah\yii2\user\models\UserToken $userToken */
$user = $this->module->model("User");
$userAuth = $this->module->model("UserAuth");
$userToken = $this->module->model("UserToken");
// attempt to find userAuth in database by id and name
$attributes = $client->getUserAttributes();
$userAuth = $userAuth::findOne(["provider" => $client->name, "provider_id" => (string) $attributes["id"]]);
if ($userAuth) {
// check if user is banned, otherwise log in
$user = $user::findOne($userAuth->user_id);
if ($user && $user->banned_at) {
return false;
}
Yii::$app->user->login($user, $this->module->loginDuration);
return true;
}
// call "setInfo{clientName}" function to ensure that we get email consistently
// this is mainly used for google auth, which returns the email in an array
// @see setInfoGoogle()
$function = "setInfo" . ucfirst($client->name);
list($user, $profile) = $this->{$function}($attributes);
// attempt to find user by email
if (!empty($user["email"])) {
// check if any user has has tried to change their email
// if so, delete it
$email = trim($user["email"]);
$userToken = $userToken::findByData($email, $userToken::TYPE_EMAIL_CHANGE);
if ($userToken) {
$userToken->delete();
}
// find user and create user provider for match
$user = $user::findOne(["email" => $email]);
if ($user) {
$userAuth = $this->initUserAuth($client);
$userAuth->setUser($user->id)->save();
Yii::$app->user->login($user, $this->module->loginDuration);
return true;
}
}
return false;
}