tutorial for registration of User
- Pavel Kravčík
- Member | 1195
You can check this, there is working example. :)
https://github.com/…/web-project
- asinkan
- Member | 38
Yes it is in examples. When you download latest Nette then the path is
…nette/examples/CD-collection
if it is already in your localhost, run www. In my case…
http://localhost/…lection/www/
do not forget to run your server.
Last edited by asinkan (2018-01-26 09:56)
- CZechBoY
- Member | 3608
OK, how to create VERY SIMPLE registration in presenter (without model, that can be extracted to another class/service)
class RegistrationPresenter extends \Nette\Application\UI\Presenter
{
private $database;
public function __construct(\Nette\Database\Context $database)
{
$this->database = $database;
}
protected function createComponentForm()
{
$form = new \Nette\Application\UI\Form();
$form->addEmail('email', 'Email')->setRequired('Please enter email');
$passwordInput = $form->addPassword('pwd', 'Password')->setRequired('Please enter password');
$form->addPassword('pwd2', 'Password (verify)')->setRequired('Please enter password for verification')->addRule($form::EQUAL, 'Password verification failed. Passwords do not match', $passwordInput);
$form->addSubmit('register', 'Register');
$form->onSuccess[] = function() use ($form) {
$values = $form->getValues();
$this->database->table('user')->insert([
'email' => $values->email,
'password_hash' => \Nette\Security\Passwords::hash($values->password),
]);
};
$form->onSucces[] = function() {
$this->redirect('Homepage:default');
};
return $form;
}
}
app/presenters/registration/default.latte
{block content}
{control form}
{/block}
btw. nette/examples on github: https://github.com/nette-examples
I will add sign up+in example to the examples repo ^^.
edit: fixed addRule parameters order
Last edited by CZechBoY (2018-01-26 22:32)
- Arthedian
- Member | 26
CZechBoY wrote:
OK, how to create VERY SIMPLE registration in presenter (without model, that can be extracted to another class/service)
class RegistrationPresenter extends \Nette\Application\UI\Presenter { private $database; public function __construct(\Nette\Database\Context $database) { $this->database = $database; } protected function createComponentForm() { $form = new \Nette\Application\UI\Form(); $form->addEmail('email', 'Email')->setRequired('Please enter email'); $passwordInput = $form->addPassword('pwd', 'Password')->setRequired('Please enter password'); $form->addPassword('pwd2', 'Password (verify)')->setRequired('Please enter password for verification')->addRule($form::EQUAL, $passwordInput, 'Password verification failed. Passwords do not match'); $form->addSubmit('register', 'Register'); $form->onSuccess[] = function() use ($form) { $values = $form->getValues(); $this->database->table('user')->insert([ 'email' => $values->email, 'password_hash' => \Nette\Security\Passwords::hash($values->password), ]); }; $form->onSucces[] = function() { $this->redirect('Homepage:default'); }; return $form; } }
app/presenters/registration/default.latte
{block content} {control form} {/block}
btw. nette/examples on github: https://github.com/nette/examples
I will add sign up+in example to the examples repo ^^.
Oh Ok, thanks :) I wasnt sure If I should use any of user function or create like this. And I should add salt or the function of hash automatically hash it too?
- duke
- Member | 650
CZechBoY wrote:
Yes, you can add some salt if you want to defend against dictionary/rainbow table attack.
Passwords::hash is not adding any other salt, it is your problem :-) It only hashes password with some hashing function.
Actually, Nette\Security\Passwords::hash uses PHP password_hash function which generates salts automatically. There is a possibility to provide salt manually via options argument, but that is deprecated in PHP 7.0.0.
- Arthedian
- Member | 26
Oh its great, that I dont have to deal with hashes. I just want to ask why this line
$form->addPassword('pwd2', 'Password (verify)')->setRequired('Please enter password for verification')->addRule($form::EQUAL, $passwordInput, 'Password verification failed. Passwords do not match');
gives me this error
Object of class Nette\Forms\Controls\TextInput could not be converted to string
?
- Arthedian
- Member | 26
oh ok. Thank everyone for the help! Below is fixed function, which is above.
protected function createComponentForm()
{
$form = new \Nette\Application\UI\Form();
$form->addEmail('email', 'Email')->setRequired('Please enter email');
$passwordInput = $form->addPassword('pwd', 'Password')->setRequired('Please enter password');
$form->addPassword('pwd2', 'Password (verify)')->setRequired('Please enter password for verification')->addRule($form::EQUAL, 'Password verification failed. Passwords do not match', $passwordInput);
$form->addSubmit('register', 'Register');
$form->onSuccess[] = function() use ($form) {
$values = $form->getValues();
$this->database->table('users')->insert([
'email' => $values->email,
'password_hash' => \Nette\Security\Passwords::hash($values->pwd),
]);
};
$form->onSuccess[] = function() {
$this->redirect('Frontend:default');
};
return $form;
}
Last edited by Arthedian (2018-01-26 16:42)