Login Status on every page

6 months 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?

6 months ago

japlavaren
Backer | 414
+
0
-

$user variable is available in each template by default

6 months ago

CZechBoY
Member | 3386
+
0
-

Use @layout.latte

6 months ago

Bill Lions
Member | 19
+
0
-

Soo, in the layout,

{php echo $user->username}

Or something?

6 months ago

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.

6 months 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>

6 months ago

MajklNajt
Member | 228
+
+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>

6 months 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?

6 months ago

manwe
Member | 44
+
0
-

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