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

6 days ago

Member | 1

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);
        } 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>';
        echo '</pre>';
        $this->db = $connection;

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

Could anyone help me?

6 days ago

David Matějka
Moderator | 4735

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)

Board footer

Terms and conditions