PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] The Auth component still fetching older value of Session?

 January 28, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I am manually modifying some key of Auth session in beforeFilter of AppController.php by this code

public function beforeFilter(Event $event){
    //$companyId = $this->Companies->find(.........
    $this->request->session()->write('Auth.User.company_id', $companyId);
}

And in various actions of different controller i'm trying to get that stored companyid in session by following way

public function add(){
    $companyId = $this->Auth->user('company_id');
    debug($companyId); die;
}

When i see the value of $companyId, it is showing older value not updated one in beforeFilter method of AppController. However if i refresh the page and donot modify session again i will get updated $companyId value.

So my question is how can i update the value of Auth session data so that i can get updated value with $this->Auth->user('company_id') code in different places in same request?


Solution

The session storage used by the authentication component uses a buffering mechanism, ie the value from the session is usually only read once per request (unless it is being deleted/emptied).

https://github.com/cakephp/cakephp/blob/3.2.8/src/Auth/Storage/SessionStorage.php#L81-L83

So either read the value directly from the session in your controller action, or do not write to the session directly, but to the session storage, something along the lines of

$user = $this->Auth->user();
if (is_array($user) && $user) {
    $user['company_id'] = $companyId;
    $this->Auth->setUser($user);
}


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing