Yii Framework Forum: Validate login between frontend and backend - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Validate login between frontend and backend Rate Topic: ***-- 1 Votes

#1 User is offline   Fabricio Castro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 08-May 17

Posted 08 August 2017 - 11:16 AM

I have a problem, already validate the admin user and the normal user. And I have it that if admin user is redirected to the backend (index). But when it is redirected to the index does not keep the session already started in the frontend and when it is in the backend it asks to login again.

My fontend/config/main.php
'components' => [
        'request' => [
            'cookieValidationKey' => '2PvWor_Yq5KViA62RaXNA3RcKqFMyL6T',
            'csrfParam' => '_csrf-frontend',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the frontend
            'name' => 'advanced-frontend',
        ],
],


My backend/config/main.php
'components' => [
        'request' => [
            'cookieValidationKey' => '-Pv9GFzUbC0eko0_jUJ9P3raNs0T_W7W',
            'csrfParam' => '_csrf-backend',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the backend
            'name' => 'advanced-backend',
        ],
],


My actionLogin in Frontend


 public function actionLogin(){
        $this->layout="main-form";
        $model = new \common\models\LoginForm();
        
        if (!Yii::$app->user->isGuest) {
           return $this->goHome();
        }
        
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            if(User::isUserAdmin()){
                return $this->redirect(Yii::$app->params['urlBackend'].'/site/index'); // redirect backend
            }else{
                return $this->render('index'); // redirect frontend
            }
        }else{
            return $this->render('login', [
                        'model' => $model,
            ]);
        }
    }


Finally My actionIndex in backend
    public function actionIndex()
    {
        if(Yii::$app->user->isGuest){
            return $this->redirect(Yii::$app->params['urlFrontend'].'/site/login');
        }
        return $this->render('index');
    }

0

#2 User is offline   Patrick Jones 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 495
  • Joined: 14-July 12
  • Location:Berlin

Posted 08 August 2017 - 02:04 PM

Your frontend and backend use different names for "identityCookie".

Also, if frontend and backend are on different subdomains, make sure to configure the same domain for "identityCookie".
Freelance developer for hire - send me a message!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users