Login Status on every page
- Bill Lions
- Member | 47
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?
- Bill Lions
- Member | 47
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
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
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?