Hi, I just want to ask , why is it that after it is authenticated it will still redirect to my login page ? the user should get inside in my site after successfully authenticated, I don’t know how to fixed this,this is how I configure the authclient in web.php
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'google' => [
'class' => 'yii\authclient\clients\GoogleOAuth',
'clientId' => 'clientid',
'clientSecret' => 'cleintsecret',
'returnUrl'=>'http://mysite.x10host.com/home/mysitex/public_html/web/site/login',
],
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => 'facebook_client_id',
'clientSecret' => 'facebook_client_secret',
],
],
],
sitcontroller.php
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'successCallback'],
],
];
}
public function successCallback($client)
{
$attributes = $client->getUserAttributes();
/** @var Auth $auth */
$auth = Auth::find()->where([
'source' => $client->getId(),
'source_id' => $attributes['id'],
])->one();
if (Yii::$app->user->isGuest) {
if ($auth) { // login
$user = $auth->user;
Yii::$app->user->login($user);
} else { // signup
if (isset($attributes['email']) && isset($attributes['username']) && User::find()->where(['email' => $attributes['email']])->exists()) {
Yii::$app->getSession()->setFlash('error', [
Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()]),
]);
} else {
$password = Yii::$app->security->generateRandomString(6);
$user = new User([
'username' => $attributes['login'],
'email' => $attributes['email'],
'password' => $password,
]);
$user->generateAuthKey();
$user->generatePasswordResetToken();
$transaction = $user->getDb()->beginTransaction();
if ($user->save()) {
$auth = new Auth([
'user_id' => $user->id,
'source' => $client->getId(),
'source_id' => (string)$attributes['id'],
]);
if ($auth->save()) {
$transaction->commit();
Yii::$app->user->login($user);
} else {
print_r($auth->getErrors());
}
} else {
print_r($user->getErrors());
}
}
}
} else { // user already logged in
if (!$auth) { // add auth provider
$auth = new Auth([
'user_id' => Yii::$app->user->id,
'source' => $client->getId(),
'source_id' => $attributes['id'],
]);
$auth->save();
}
}
}