Login Status on every page

about a month ago

Bill Lions
Member | 19
+
0
-

I wish to put the login status of a user in the top bar of my site.
This is simple enough, but how to share this logic across multiple presenters, and to the template, without reproducing the code for each presenter?

about a month ago

japlavaren
Backer | 412
+
0
-

$user variable is available in each template by default

about a month ago

CZechBoY
Member | 3334
+
0
-

Use @layout.latte

about a month ago

Bill Lions
Member | 19
+
0
-

Soo, in the layout,

{php echo $user->username}

Or something?

about a month ago

manwe
Member | 39
+
+1
-

instead of {php echo $user->username} you can just use {$user->username}.

But check the $user variable to check whats inside.
Its filled by authenticator.

about a month ago

Bill Lions
Member | 19
+
0
-

I get an error

Nette\MemberAccessException
Cannot read an undeclared property Nette\Security\User::$username

My authenticator is basically a copy from the tutorial on https://doc.nette.org/…cess-control

class UserAuthenticator implements Nette\Security\IAuthenticator
{
        private $database;

        public function __construct(Nette\Database\Context $database)
        {
                $this->database = $database;
        }

        public function authenticate(array $credentials)
        {
                list($username, $password) = $credentials;
                $row = $this->database->table('users')
                        ->where('username', $username)->fetch();

                if (!$row) {
                        throw new Nette\Security\AuthenticationException('User not found.');
                }

                if (!Nette\Security\Passwords::verify($password, $row->password)) {
                        throw new Nette\Security\AuthenticationException('Invalid password.');
                }

                return new Nette\Security\Identity($row->id, $row->role, ['username' => $row->username]);
        }
}

My latte template has

<li>{$user->username}</li>

about a month ago

MajklNajt
Member | 168
+
+1
-

you should use {$user->getIdentity()->username}

Bill Lions wrote:

I get an error

Nette\MemberAccessException
Cannot read an undeclared property Nette\Security\User::$username

My authenticator is basically a copy from the tutorial on https://doc.nette.org/…cess-control

class UserAuthenticator implements Nette\Security\IAuthenticator
{
        private $database;

        public function __construct(Nette\Database\Context $database)
        {
                $this->database = $database;
        }

        public function authenticate(array $credentials)
        {
                list($username, $password) = $credentials;
                $row = $this->database->table('users')
                        ->where('username', $username)->fetch();

                if (!$row) {
                        throw new Nette\Security\AuthenticationException('User not found.');
                }

                if (!Nette\Security\Passwords::verify($password, $row->password)) {
                        throw new Nette\Security\AuthenticationException('Invalid password.');
                }

                return new Nette\Security\Identity($row->id, $row->role, ['username' => $row->username]);
        }
}

My latte template has

<li>{$user->username}</li>

about a month ago

Bill Lions
Member | 19
+
0
-

So, that gets rid of the error, but does not show the value.
Is there something in the UserAuthenticator that I am doing/not doing to cause this?

about a month ago

manwe
Member | 39
+
0
-

dump the $user variable to Tracy using
{dump $user}
to see whats inside, if everything is set correctly