title: Yii2 user identity gets null after login
body: <p>I have a Yii2 application in which I am authenticating user from third party API and once user is authenticated then I login the user and set identity and everything works fine. But as I navigate to any other page user identity gets null and user becomes Guest user.</p>
  <p>Here is my login form:</p>
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use app\components\RestBehavior;
  use app\models\web\LoginIdentity;
  use Yii;
  use yii\base\Model;
  use yii\helpers\Json;
  use yii\helpers\VarDumper;
  use yii\web\IdentityInterface;
  
  /**
   * LoginForm is the model behind the login form.
   *
   * @property User|null $user This property is read-only.
   *
   */
  class LoginForm extends Model
  {
      public $email;
      public $password;
      public $company_id;
      public $companies=[];
      public $token;
  
      private $_user = false;
  
  
      public function behaviors()
      {
          return [
              RestBehavior::class
          ];
      }
  
      /**
       * @return array the validation rules.
       */
      public function rules()
      {
          return [
              [['email', 'password','company_id'], 'required'],
          ];
      }
  
      /**
       * Logs in a user using the provided username and password.
       * @return bool whether the user is logged in successfully
       */
      public function login()
      {
          return Yii::$app-&gt;user-&gt;login($this-&gt;getUser(), 0);
      }
  
      /**
       * Finds user by [[username]]
       *
       * @return User|null
       */
      public function getUser()
      {
          if ($this-&gt;_user === false) {
              $this-&gt;_user = LoginIdentity::findUser($this-&gt;attributes);
          }
          return $this-&gt;_user;
      }
  
  }
  </code></pre>
  <p>Here is my custom LoginIdentity class:</p>
  <pre><code>&lt;?php
  
  namespace app\models\web;
  
  use app\components\RestBehavior;
  use \Yii;
  use yii\helpers\Json;
  use yii\helpers\Url;
  use yii\helpers\VarDumper;
  
  class LoginIdentity extends \yii\base\BaseObject implements \yii\web\IdentityInterface
  {
      public $resource_id;
      public $token;
  
      /**
       * {@inheritdoc}
       */
      public function behaviors()
      {
          return [
              RestBehavior::class
          ];
      }
  
      public static function findUser($params)
      {
          if(!empty(Yii::$app-&gt;user-&gt;identity))
          {
              return new static([
                  'resource_id'=&gt;self::getId(),
                  'token'=&gt;self::getAuthKey()
              ]);
          }
          else
          {
              Yii::$app-&gt;controller-&gt;sendPOST('/login',$params);
              if(Yii::$app-&gt;response-&gt;statusCode===200)
              {
                  $response=Json::decode(Yii::$app-&gt;response-&gt;data);
                  $identity=new static([
                      'resource_id'=&gt;$response['resource']['resource_id'],
                      'token'=&gt;$response['token']
                  ]);
                  Yii::$app-&gt;session-&gt;set('resource_id',$response['resource']['resource_id']);
                  Yii::$app-&gt;session-&gt;set('token',$response['token']);
                  return $identity;
              }
              return null;
          }
  
      }
  
      /**
       * {@inheritdoc}
       */
      public static function findIdentity($id)
      {
          $session=Yii::$app-&gt;session;
          if(!empty($session['resource_id'])&amp;&amp;!empty($session['token']))
          {
              return new static([
                  'resource_id'=&gt;$session['resource_id'],
                  'token'=&gt;$session['token']
              ]);
          }
          return null;
      }
  
      /**
       * {@inheritdoc}
       * @param \Lcobucci\JWT\Token $token
       */
      public static function findIdentityByAccessToken($token, $type = null)
      {
      }
  
      /**
       * Finds user by username
       *
       * @param string $username
       * @return static|null
       */
      public static function findByUsername($username)
      {
  
      }
  
      /**
       * {@inheritdoc}
       */
      public function getId()
      {
          return $this-&gt;resource_id;
      }
  
      /**
       * {@inheritdoc}
       */
      public function getAuthKey()
      {
          return $this-&gt;token;
      }
  
      /**
       * {@inheritdoc}
       */
      public function validateAuthKey($authKey)
      {
      }
  
      /**
       * Validates password
       *
       * @param string $password password to validate
       * @return bool if password provided is valid for current user
       */
      public function validatePassword($password)
      {
      }
  }
  </code></pre>
  <p>Very strangely, the whole code works fine on my local computer i.e on localhost. But on server user get's logged in but logs out as he visits any new authenticated route. I have checked server's /var/lib/php/sessions folder, the session gets saved properly, so there are no permission issues.
  Can anyone please tell me what is wrong here? Why it is working on my local computer and not remotely.
  Thank you.</p>