1

In CakePHP - prior to version 4.x - it had a simple Auth component which dealt with setting up authentication/authorization and made it very easy to do things like get a logged-in user ID with one line of code.

In 4.x the Auth component has been dropped and split out into separate Authentication and Authorization components.

There seems to be a lack of information about how you use these two new components (the docs are very sparse and the README's on GitHub provide little of use).

What I've managed to do so far is:

Followed the CMS Tutorial to set up a CakePHP 4.3.7 app with both Authentication and Authroization.

In a Controller method I added the following. I was only able to find this because somebody had posted an updated answer on Cakephp - get user id in controller when using Auth - it doesn't actually seem to be documented anywhere:

$identity = $this->request->getAttribute('authentication')->getIdentity();
debug($identity);

This gives:

object(Authentication\Identity) id:0 {
    'config' => [ ]
    'data' => object(App\Model\Entity\User) id:1 {
        'id' => (int) 7
        'first_name' => 'Andy'
        'last_name' => 'My Last Name'
        'email' => 'andy@example.com'

If I do debug($identity->data->id); I'd expect to be able to read the id property of the object and have it return my user ID, 7 in this case. It gives null.

I can't help but think this is the wrong way to do it and there should be an easy method to just return the logged in user ID given this is almost always going to be required in any use-case.

Please can somebody either point to where this is in the docs, or advise how it can be achieved?

Andy
  • 5,142
  • 11
  • 58
  • 131
  • Have you checked **https://book.cakephp.org/authentication/2/en/index.html**? – ndm Apr 13 '22 at 12:40
  • @ndm I've found the answer (elsewhere). The Cake docs like to make things as convoluted as possible. The only reference I can find on that the page above is that it has a link to another page on Identity where you may piece together what would be required. Given obtaining a user ID is one of the most obvious/common things you'd want to do after somebody has logged in, I don't know why this can't just be made very clear or even have a well-named method (`getUserId()` or such). – Andy Apr 13 '22 at 13:32

2 Answers2

1

You can get the authenticated user identity data using the authentication component:

$user = $this->Authentication->getIdentity();

You can also get the identity directly from the request instance:

$user = $request->getAttribute('identity');

It's right here in the docs: https://book.cakephp.org/authentication/2/en/authentication-component.html#accessing-the-logged-in-user

Spriz
  • 457
  • 2
  • 9
  • You are linking to the v2 documentation, whereas cakephp 4 was specifically mentioned in the question. – Axel Köhler Jan 27 '23 at 15:59
  • 1
    @AxelKöhler actually the docs is for v2 of the Authentication plugin which is the one in use for CakePHP 4.x :-) – Spriz Jan 30 '23 at 14:47
0

I found an answer which I'm posting because it works although I'm not 100% sure if this is the correct way.

If you have another way please post an answer.

The Stack Overflow link I posted in the original question is misleading; this doesn't do what I need.

I came across https://discourse.cakephp.org/t/display-the-data-of-a-logged-in-user/8160 which suggests you can use:

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

You can then read properties of this, e.g.

$user->id; // In my example this returns (int)7

You can also read any other properties of the user that are in the users database table, e.g.

echo $user->first_name;
// "Andy"
Andy
  • 5,142
  • 11
  • 58
  • 131