tutorial for registration of User

10 months ago

Arthedian
Member | 26
+
0
-

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

10 months ago

Pavel Kravčík
Member | 944
+
0
-

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

10 months ago

asinkan
Member | 28
+
+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)

10 months ago

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 :(

10 months ago

asinkan
Member | 28
+
0
-

Download nette from here

inside is …examples/CD-collection

Do not use composer.

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

10 months ago

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.

10 months ago

asinkan
Member | 28
+
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)

10 months ago

CZechBoY
Member | 3205
+
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)

10 months ago

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?

10 months ago

CZechBoY
Member | 3205
+
-4
-

-- removed as it was not true --

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

10 months ago

duke
Member | 627
+
+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.

10 months ago

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?

10 months ago

David Matějka
Moderator | 5695
+
+1
-

$passwordInput should be third parameter

10 months ago

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)