tags:

views:

164

answers:

2

Hi,

I managed to get authentication to work by following the tutorial from this page http://planetcakephp.org/aggregator/items/2604-create-simple-user-authentication-using-cakephp%E2%80%99s-auth-component

How do I make sure when I'm in the register page, and the password and confirm password the user supplied doesn't match, the password field will be repopulated with the value the user enter and not the hashed version of it?

Thanks,
Tee

A: 

I don't think it's necessary to figure this out because of the feature of password input.Also it would be difficult to do this since you've used the Auth component.From the cookbook:

The auth component will automatically hash the password field if the username field is also present in the submitted data

That means you will lost the original password data after your submit it.However,I think there's a tricky way to approach that with javascript:adding a hidden type input in the register view file which is the same value but different name with $form->input('password');,then you can retrieve it in the action to display in the password input text.e.g

if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) 
{
    $this->User->save($this->data);
    $this->Session->setFlash("your data has been saved.");
    $this->redirect("index");
}
else
{
    $this->data['User']['password'] = $this->data['User']['trickpassword'];
    /*i prefer this
    $this->data['User']['password'] = '';
    $this->data['User']['password_confirm'] = '';
    */
    $this->Session->setFlash('Password confirm fail!');
}
SpawnCxy
I prefer `$this->User->invalidate('trickpassword','Password confirm fail!')` to a session var, as it fits better with the form-validation idiom and allows your form-validation logic to return other errors simultaneously.
Daniel Wright
@ Daniel Wright,good note:)
SpawnCxy
+1  A: 

Instead of using trickpassword, you already have the password_confirm field with the unhashed password. You can use rules similar to these to check the password field against blank passwords... And then hash the password_confirm to make sure the two are the same.

http://bin.cakephp.org/saved/42156

TehTreag