Answer the question
In order to leave comments, you need to log in
Is it possible to implement subdomain authorization in yii2?
Hello!
Problem following, it is necessary to implement cross domain authorization.
The scheme is as follows:
1. On the first domain (user.mysite.com) there is a user's personal account that works on yii2 and through which authorization will pass.
2. On the second domain (mysite.com) there is an online store on a "self-written" engine that must work with an authorized user.
3. They have one database.
4. They lie on the same server, in neighboring directories.
5. the "remember me" option should be implemented Tell me
, can this be made to work? And what is the best way to do it?
Answer the question
In order to leave comments, you need to log in
If you have a custom engine, then you need to take data from Yii::$app->user->isGuest and Yii::$app->user->identity.
It is advisable to create a separate config without unnecessary components so that there is no superfluous in components, because you don’t need extra load. Next, in your engine (in session) paste:
<?php
//Указываете правильные нижеидущие пути, относительно вашего движка
require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php'); //Если есть, путь до общего файла с алиасами
require(__DIR__ . '/../config/bootstrap.php'); //Если есть, путь до файла алиасов приложения
//Тут у вас ваш минимальный кофиг, без всего лишнего, обязательно в компонентах должен быть указан ваш класс User
$config = require(__DIR__ . '/../config/main.php');
//Создаем объект приложения
$app = new yii\web\Application($config);
//Вытаскиваем класс юзер
if (!$app->getUser()->isGuest) {
echo 'Авторизирован!';
} else {
echo 'Не авторизирован!';
}
//Вытаскиваем класс юзер нашего юи приложения, и в нем уже будут данные об авторизированном юзере
$userData = $app->getUser()->identity;
general authorization for an application on a subdomainx works with a bang, without dancing with a tambourine, it is enough to specify the same
'cookieValidationKey'
Let's say there are 2 sites:
site.ru and subdomain.site.ru
On site.ru, the configuration should be like this
'components' => [
....
'session' => ['cookieParams' => ['domain' => 'site.ru', 'httpOnly' => true]],
'request' => [
'cookieValidationKey' => 'КлючОдинаковый',
],
'user' => [
'identityClass' => 'app\models\User',
'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => 'site.ru'],
'autoRenewCookie' => true,
....
],
....
]
'components' => [
....
'session' => ['cookieParams' => ['domain' => '.site.ru', 'httpOnly' => true]],
'request' => [
'cookieValidationKey' => 'КлючОдинаковый',
],
'user' => [
'identityClass' => 'app\models\User',
'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => '.site.ru'],
'autoRenewCookie' => true,
....
],
....
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question