How to login using two different model or switch identity class in yii2?
<p>I want to allow user login from two different model.</p>

<p><em>Config.php</em></p>

<pre><code>'user' =&gt; [
        'identityClass' =&gt; 'app\models\User', //one more class here
        'enableAutoLogin' =&gt; false,
        'authTimeout' =&gt; 3600*2,
    ],
</code></pre>

<p><em>LoginForm.php</em></p>

<pre><code> public function rules()
{
    return [
        // username and password are both required
        [['username', 'password'], 'required'],
        // rememberMe must be a boolean value
        ['rememberMe', 'boolean'],
        // password is validated by validatePassword()
        ['password', 'validatePassword'],
    ];
}

 public function validatePassword($attribute, $params)
{
    if (!$this-&gt;hasErrors()) {
        $user = $this-&gt;getUser();

        if (!$user || !$user-&gt;validatePassword($this-&gt;password)) {
            $this-&gt;addError($attribute, Yii::t('user', 'Incorrect username or password.'));
        }
    }
}

public function login()
{
    if ($this-&gt;validate()) {
        return Yii::$app-&gt;user-&gt;login($this-&gt;getUser(), $this-&gt;rememberMe ? 3600*24*30 : 0);
    } else {
        return false;
    }
}

public function parentLogin()
{
    // How to validate parent Login?
}

public function getUser()
{
    if ($this-&gt;_user === false) {
        $this-&gt;_user = User::findByUsername($this-&gt;username);
    }

    return $this-&gt;_user;
}
</code></pre>

<p><em>User.php</em></p>

<pre><code>class User extends \yii\db\ActiveRecord implements IdentityInterface
{
    public static function tableName()
   {
    return 'users';
   }

   public static function findIdentity($id)
  {
    return static::findOne($id);
  }

  public static function findByUsername($username)
 {
    return static::findOne(['user_login_id' =&gt; $username]);
 }
</code></pre>

<p><em>Controller.php</em></p>

<pre><code> public function actionLogin()
{
    // Working
}

public function actionParentLogin()
{
    $model = new LoginForm();

    if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;parentLogin()) {

            $parent = ParentLogin::find()-&gt;where(['p_username' =&gt; $model-&gt;p_username])-&gt;one();
        if($parent){
            \Yii::$app-&gt;session-&gt;set('p_id',$parent-&gt;p_id);
            return $this-&gt;redirect(['parent-dashboard']);
        }
        else
        {
            Yii::$app-&gt;getSession()-&gt;setFlash('error', Yii::t('site', 'Incorrect username or password.'));
        }
    }
    return $this-&gt;render('parent-login', [
            'model' =&gt; $model,
        ]);
}
</code></pre>

<p>I don't know how to validate parent login. I tried for hours finding workaround but not succeed.</p>

<p>I am stuck on <code>Yii::$app-&gt;user-&gt;login($this-&gt;getUser(), $this-&gt;rememberMe ? 3600*24*30 : 0);</code> because user table don't have parent login records. </p>

<p><strong>My questions</strong></p>

<p>1) Is it possible to have two <code>identityClass</code>. If yes then how? <br>
2) Is it possible to extend <code>ParentLogin</code> model to <code>User</code>. If yes then how to validate?</p>

<p><strong>References</strong></p>

<p><a href="https://github.com/dektrium/yii2-user/issues/6" rel="nofollow noreferrer">How To extend user class?</a><br>
<a href="http://chris-backhouse.com/cook-your-own-user-authentication-in-yii-part-2/990" rel="nofollow noreferrer">Customising the CWebUser class</a><br>
<a href="https://stackoverflow.com/questions/27836662/custom-useridentity-class-in-yii2">Custom userIdentity class in yii2</a></p>