Код моего контроллера. Оставил только то, что отвечает за регистрацию.
Код: Выделить всё
<?php
namespace backend\modules\user\controllers;
use common\modules\user\models\Account;
use common\modules\user\models\forms\LoginForm;
use common\modules\user\models\forms\RegisterForm;
use common\modules\user\models\User;
use common\modules\user\Module;
use Yii;
use yii\authclient\AuthAction;
use yii\authclient\ClientInterface;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\web\Controller;
class AuthController extends Controller
{
public function actions()
{
return [
'auth' => [
'class' => AuthAction::className(),
'successCallback' => [$this, 'authenticate']
],
];
}
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'register', 'request-password-reset', 'reset-password'],
'allow' => true,
'roles' => ['?']
],
[
'actions' => ['auth', 'connect'],
'allow' => true,
'roles' => ['?', '@']
],
['actions' => ['logout'], 'allow' => true, 'roles' => ['@']]
]
]
];
}
public function actionLogin()
{
if (!Yii::$app->getUser()->getIsGuest()) {
$this->goHome();
}
$model = new LoginForm;
if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', ['model' => $model]);
}
}
public function actionLogout()
{
Yii::$app->getUser()->logout();
return $this->goHome();
}
public function actionRegister()
{
$model = new RegisterForm();
if ($model->load(Yii::$app->getRequest()->post())) {
if ($user = $model->register()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('register', ['model' => $model]);
}
public function actionConnect($providerId)
{
$model = new RegisterForm();
$account = Account::find()->where([
'user_id' => null,
'provider_id' => $providerId
])->one();
if ($account && $model->load(Yii::$app->getRequest()->post())) {
if ($user = $model->register()) {
$user->link('accounts', $account);
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('register', ['model' => $model]);
}
public function authenticate(ClientInterface $client)
{
$provider = $client->getId();
$attributes = $client->getUserAttributes();
/** @var User $identity */
$identity = Yii::$app->getUser()->getIdentity();
$account = Account::find()->where([
'provider' => $provider,
'provider_id' => $attributes['id']
])->one();
if (!$account) {
$account = new Account;
$account->setAttributes([
'provider' => $provider,
'provider_id' => $attributes['id'],
'data' => json_encode($attributes, JSON_UNESCAPED_UNICODE)
], false);
if ($identity) {
$account->setAttribute('user_id', $identity->id);
}
$account->save(false);
}
if ($account->user) {
if ($identity) {
Yii::$app->getSession()->setFlash('success', $provider . ' ' . Module::t('is connected'));
$this->action->successUrl = Url::toRoute(['/user/default/update', 'id' => $identity->id]);
}
Yii::$app->getUser()->login($account->user, 3600 * 24 * 30);
} else {
$this->action->successUrl = Url::toRoute(['/user/auth/connect', 'providerId' => $attributes['id']]);
}
}
}