Login Status on every page

Bill Lions
Member | 47
+
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?

japlavaren
Member | 404
+
0
-

$user variable is available in each template by default

CZechBoY
Member | 3608
+
0
-

Use @layout.latte

Bill Lions
Member | 47
+
0
-

Soo, in the layout,

{php echo $user->username}

Or something?

manwe
Member | 44
+
+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.

Bill Lions
Member | 47
+
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/…thentication

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>
MajklNajt
Member | 494
+
+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/…thentication

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>
Bill Lions
Member | 47
+
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?

manwe
Member | 44
+
0
-

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