O
O
Orc6662016-07-25 19:12:31
Yii
Orc666, 2016-07-25 19:12:31

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

3 answer(s)
A
Andrew, 2016-07-25
@mhthnz

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;

Then you combine the whole thing with your engine.

M
Maxim Timofeev, 2016-07-26
@webinar

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'

J
Jorik86, 2018-10-05
@Jorik86

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,
            ....
    ],
  ....
]

On subdomain.site.ru, the configuration should be like this. In fact, the same thing, only for cookies you need to change the domain parameter.
'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,
            ....
    ],
  ....
]

Well, app\models\User should work either with one table of users. (or its replica)
Now log in to site.ru and go to subdomain.site.ru, Yii::$app->user->id must be set.
https://yii2-cookbook.readthedocs.io/cookies/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question