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

Thursday, December 30, 2021

[FIXED] Cakephp 4 - Authentication Plugin how to fetch current user name?

 December 30, 2021     cakephp, php     No comments   

Issue

I'm using CMS Authentication plugin

I fetch current user name like this but when I update user profile the current user name doesn't get reflected I need to logout first

    $user = $this->request->getAttribute('identity');
    $name = $user->first_name ?? '';

Solution

How to @ CakePHP 4.x

update identity object with: $this->Authentication->setIdentity($user);

Full example:

public function editMe()
{
    // fetch current identity data
    $user = $this->Authentication->getIdentity()->getOriginalData();
    if ($this->request->is(['patch', 'post', 'put'])) {
        $user = $this->Users->patchEntity($user, $this->getRequest()->getData());
        if ($this->Users->save($user)) {
            $this->Authentication->setIdentity($user); // ----- > update identity data
            $this->Flash->success(__('The user has been saved.'));

            return $this->redirect(['action' => 'me']);
        }
        $this->Flash->error(__('The user could not be saved. Please try again.'));
    }
    $this->set(compact('user'));
}


Answered By - Salines
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

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