Sunday, January 3, 2010

Re: Login after registration not working

(Kind of) SOLUTION

I've kept tinkering and noticed that the log in query was trying to
use the unhashed password. So, I changed the register method from
this:
...
// log the user in to his new account
$this->data['User']['password'] = $this->data['User']['password1'];
...

to this:
...
// log the user in to his new account
$this->data['User']['password'] = $this->Auth->password($this->data
['User']['password1']);
...

This did the trick. But, I'm surprised. I thought Auth was supposed to
hash the password automagically. If I'm right about that, then I must
be still doing something wrong and would love any insight.

On Jan 3, 11:38 am, OohBunToo <gcull...@gmail.com> wrote:
> Hey all,
>
> I'm trying to set up a user registration system that uses the Auth
> component. I've set up both the registration and login parts of my
> site, and separately each work as expected (i.e. a user can register
> and is saved to the database, and a user can log in separately once
> registered). But, I can't get seem to get the Auth component to log in
> users automatically once they've registered.
>
> I've poured over Google searches, the cookbook, the API documentation,
> but nothing seems to get me around my issue.
>
> Here's the beforeFilter in my app_controller:
> function beforeFilter() {
>     // Override the default fields used by Auth component
>     $this->Auth->fields = array(
>         'username'=>'email',
>         'password'=>'password'
>     );
>
> Here are the beforeFilter and register methods in my users_controller:
> function beforeFilter() {
>     parent::beforeFilter();
>     $this->Auth->allow(array('register', 'login', 'logout'));
>
> }
>
> function register() {
>     if($this->Session->read('User.signedIn')) {
>         $this->redirect(array('action'=>'account'));
>     }
>
>     if (!empty($this->data)) {
>         $this->User->create();
>         if ($this->User->save($this->data)) {
>             // log the user in to his new account
>             $this->data['User']['password'] = $this->data['User']
> ['password1'];
>             $this->Session->setFlash(__('Hooray! The User has been
> saved', true));
>             $this->Auth->login($this->data);
>             $this->redirect($this->Auth->redirect());
>         } else {
>             $this->Session->setFlash(__('Uh oh! That didn\'t work.
> Please, try again.', true));
>         }
>     }
>
> }
>
> The redirect takes place, but the user isn't actually getting logged-
> in. Instead, they're dropped onto the login form with the
> "Hooray! ..." flash message as well as the "You are not authorized..."
> Auth flash message. So, it's clear that the $this->Auth->login($this-
>
> >data) line isn't doing what's expected.
>
> I've confirmed that the $this->data array contains the right keys and
> values using debug:
> app/controllers/users_controller.php (line 31)
>
> Array
> (
>     [User] => Array
>         (
>             [email] => asdfi...@grr.com
>             [password1] => aaa
>             [password2] => aaa
>             [password] => aaa
>         )
> )
>
> So, I'm out of ideas. I really want to use the Auth component, but the
> learning curve is giving me headaches. Any help will be hugely
> appreciated!

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: