Discussion:
Auth component not returning user data on manual login (CakePHP 2.0.3)
Pratz_
2011-11-24 07:42:32 UTC
Permalink
Hi,
I have been trying to manually login a user in version 2.0.3 using:
$this->Auth->login($data).

Though it successfully logins the user, and can read user data through
$this->Session->read('Auth.User.id'), the Auth Component does not
return the user data. $this->Auth->user('id') returns NULL.

Thus login through cookies [using remember me] or automatic logging in
of a newly created user does not work.

May be I am doing something wrong, please provide some leads.

Thanks,
Pratz_
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe-/***@public.gmane.org For more options, visit this group at http://groups.google.com/group/cake-php
elitalon
2011-11-25 07:01:01 UTC
Permalink
If `$data` is an array built like as if it was the result of a
`find('first')`, i.e. indexed by your user model name, try using

`$this->Auth->login($data['User'])`

instead of

`$this->Auth->login($data)`
Post by Pratz_
Hi,
$this->Auth->login($data).
Though it successfully logins the user, and can read user data through
$this->Session->read('Auth.User.id'), the Auth Component does not
return the user data. $this->Auth->user('id') returns NULL.
Thus login through cookies [using remember me] or automatic logging in
of a newly created user does not work.
May be I am doing something wrong, please provide some leads.
Thanks,
Pratz_
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe-/***@public.gmane.org For more options, visit this group at http://groups.google.com/group/cake-php
Pratz_
2011-11-28 07:08:59 UTC
Permalink
I tried doing: $this->Auth->login($data['User']);
It too successfully logins the user, but the user data is still not
accessible via Auth component.

$this->Auth->user('id') still returns NULL.
Post by elitalon
If `$data` is an array built like as if it was the result of a
`find('first')`, i.e. indexed by your user model name, try using
    `$this->Auth->login($data['User'])`
instead of
    `$this->Auth->login($data)`
Post by Pratz_
Hi,
$this->Auth->login($data).
Though it successfully logins the user, and can read user data through
$this->Session->read('Auth.User.id'), the Auth Component does not
return the user data. $this->Auth->user('id') returns NULL.
Thus login through cookies [using remember me] or automatic logging in
of a newly created user does not work.
May be I am doing something wrong, please provide some leads.
Thanks,
Pratz_
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe-/***@public.gmane.org For more options, visit this group at http://groups.google.com/group/cake-php
elitalon
2011-11-28 07:43:21 UTC
Permalink
According to documentation (http://api20.cakephp.org/class/auth-
component#method-AuthComponentlogin) if you login a user with a custom
array, only the information in that array is populated in
AuthComponent. Thus, if you want to retrieve the ID you must ensure
that the ID is present in `$data['User]`.

If you're manually login a user after creating it in a POST request
you can use this after saving the user:

$this->request->data['User']['id'] = $this->User->id;
$this->Auth->login($this->request->data['User']);
Post by Pratz_
I tried doing: $this->Auth->login($data['User']);
It too successfully logins the user, but the user data is still not
accessible via Auth component.
$this->Auth->user('id') still returns NULL.
Post by elitalon
If `$data` is an array built like as if it was the result of a
`find('first')`, i.e. indexed by your user model name, try using
    `$this->Auth->login($data['User'])`
instead of
    `$this->Auth->login($data)`
Post by Pratz_
Hi,
$this->Auth->login($data).
Though it successfully logins the user, and can read user data through
$this->Session->read('Auth.User.id'), the Auth Component does not
return the user data. $this->Auth->user('id') returns NULL.
Thus login through cookies [using remember me] or automatic logging in
of a newly created user does not work.
May be I am doing something wrong, please provide some leads.
Thanks,
Pratz_
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe-/***@public.gmane.org For more options, visit this group at http://groups.google.com/group/cake-php
Pratz_
2011-11-28 08:30:17 UTC
Permalink
Ah, that did the trick. Thanks!
Post by elitalon
According to documentation (http://api20.cakephp.org/class/auth-
component#method-AuthComponentlogin) if you login a user with a custom
array, only the information in that array is populated in
AuthComponent. Thus, if you want to retrieve the ID you must ensure
that the ID is present in `$data['User]`.
If you're manually login a user after creating it in a POST request
    $this->request->data['User']['id'] = $this->User->id;
    $this->Auth->login($this->request->data['User']);
Post by Pratz_
I tried doing: $this->Auth->login($data['User']);
It too successfully logins the user, but the user data is still not
accessible via Auth component.
$this->Auth->user('id') still returns NULL.
Post by elitalon
If `$data` is an array built like as if it was the result of a
`find('first')`, i.e. indexed by your user model name, try using
    `$this->Auth->login($data['User'])`
instead of
    `$this->Auth->login($data)`
Post by Pratz_
Hi,
$this->Auth->login($data).
Though it successfully logins the user, and can read user data through
$this->Session->read('Auth.User.id'), the Auth Component does not
return the user data. $this->Auth->user('id') returns NULL.
Thus login through cookies [using remember me] or automatic logging in
of a newly created user does not work.
May be I am doing something wrong, please provide some leads.
Thanks,
Pratz_
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe-/***@public.gmane.org For more options, visit this group at http://groups.google.com/group/cake-php
Loading...