tutorial for registration of User

Arthedian
Member | 26
+
0
-

Hi,
is there any tutorial for registration of user? I only found for login and custom authorizator here.

Pavel Kravčík
Member | 1195
+
0
-

You can check this, there is working example. :)
https://github.com/…/web-project

asinkan
Member | 38
+
+1
-

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)

Arthedian
Member | 26
+
0
-

I Downloaded the Github repository and install it through composer, but in neither the original repository or install project the isn't any folder named “CD-collection” and I can't find any examples :(

asinkan
Member | 38
+
0
-

Download nette from here

inside is …examples/CD-collection

Do not use composer.

Last edited by asinkan (2018-01-26 11:04)

Arthedian
Member | 26
+
0
-

OK I found it. But still I can see only log in and logout example, but not register of the user.

asinkan
Member | 38
+
0
-

Ah, ok. Sorry. But form for adding a new album is very similar to register a new user, not?

Last edited by asinkan (2018-01-26 12:50)

CZechBoY
Member | 3608
+
0
-

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
+
0
-

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?

CZechBoY
Member | 3608
+
-4
-

-- removed as it was not true --

Last edited by CZechBoY (2018-01-26 22:29)

duke
Member | 650
+
+3
-

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
+
0
-

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?

David Matějka
Moderator | 6445
+
+1
-

$passwordInput should be third parameter

Arthedian
Member | 26
+
0
-

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)