Nette\InvalidStateException – Service User has not been set. – login form

Notice: This thread is very old.
ziomski
Member | 7
+
0
-

Hello,
I've created simple login form, which look's like this:

namespace App\Presenters;

use Nette;
use Nette\Application\UI\Form;
use Nette\Security\User;

class SignPresenter extends Nette\Application\UI\Presenter {

    protected function createComponentSignInForm() {
        $form = new Form;
        $form->addText('username', 'Username:')
                ->setRequired('Please enter your username.');

        $form->addPassword('password', 'Password:')
                ->setRequired('Please enter your password.');

        $form->addSubmit('send', 'Sign in');

        $form->onSuccess[] = [$this, 'signInFormSucceeded'];
        return $form;
    }

    public function signInFormSucceeded($form, $values) {
        try {
            $this->getUser()->login($values->username, $values->password);
            $this->redirect('Charts:default');
        } catch (Nette\Security\AuthenticationException $e) {
            $form->addError('Złe hasło.');
        }
    }

}

and then, in Charts presenter, I'm checking if the user is logged in:

namespace App\Presenters;

use Nette;
use App\Model;
use Nette\Security\User;

class ChartsPresenter extends Nette\Application\UI\Presenter {

    protected $db;

    public function __construct(\DibiConnection $connection) {
        echo '<pre>';
        print_r($this->getUser());
        echo '</pre>';
        die();
        $this->db = $connection;
    }
}

and I've got an exception:
Nette\InvalidStateException
Service User has not been set.

Could anyone help me?
Pawel

David Matějka
Moderator | 6445
+
+1
-

Hi, user service is not available in a constructor. in a constructor you should just set dependencies etc.

for checking whether user is logged in, use startup() method (or checkRequirements)