Hi, Trying to use the library I got such an error.Where am I doing wrong?
@jasny, @SergeAx, @Minstel
Fatal error: Uncaught Error: Typed property Veloster\App\Models\User::$hashedPassword must not be accessed before initialization in C:\xampp\htdocs\php\App\Models\User.php:32
Stack trace:
#0 C:\xampp\htdocs\php\vendor\jasny\auth\src\Auth.php(319): Veloster\App\Models\User->verifyPassword('12345678')
#1 C:\xampp\htdocs\php\App\Models\Accounts.php(46): Jasny\Auth\Auth->login('admin', '12345678')
#2 C:\xampp\htdocs\php\App\Controllers\Home.php(27): Veloster\App\Models\Accounts->authenticate('admin', '12345678')
#3 [internal function]: Veloster\App\Controllers\Home::login(
#4 C:\xampp\htdocs\php\Core\Route.php(74): call_user_func_array(Array, Array)
#5 C:\xampp\htdocs\php\index.php(24): Veloster\Core\Route::dispatch()
#6 {main}
thrown in C:\xampp\htdocs\php\App\Models\User.php on line 32
User.php:
<?php
namespace Veloster\App\Models;
use Jasny\Auth;
use Jasny\Auth\UserInterface;
class User implements UserInterface
{
public string $id;
public string $username;
public int $accessLevel = 0;
protected string $hashedPassword;
public function getAuthId(): string
{
return $this->id;
}
/**
* {@interal This method isn't required by the interface}}.
* @param string $password
*/
public function changePassword(string $password): void
{
$this->hashedPassword = password_hash($password, PASSWORD_BCRYPT);
}
public function verifyPassword(string $password): bool
{
return password_verify($password, $this->hashedPassword);
}
public function getAuthChecksum(): string
{
return hash('sha256', $this->id . $this->hashedPassword);
}
public function getAuthRole(Auth\ContextInterface $context = null): int
{
return $this->accessLevel;
}
public function requiresMfa(): bool
{
return false;
}
}
Funstion in Login_Controller.php:
public function authenticate(string $username,string $password)
{
$levels = new Authz\Levels(['user' => 1, 'moderator' => 10, 'admin' => 100]);
$auth = new Auth($levels, new AuthStorage());
session_start();
$auth->initialize();
$auth->login($username,$password);
}
AuthStroge.php:
<?php
namespace Veloster\App\Models;
use Jasny\Auth;
use Illuminate\Database\Capsule\Manager as DB;
use Jasny\Auth\StorageInterface;
class AuthStorage implements StorageInterface
{
/**
* Fetch a user by ID
* @param string $id
* @return Auth\UserInterface|null
*/
public function fetchUserById(string $id): ?Auth\UserInterface
{
DB::table('Accounts')
->Where('id','=', $id)
->get();
return new User();
}
/**
* Fetch a user by username
* @param string $username
* @return Auth\UserInterface|null
*/
public function fetchUserByUsername(string $username): ?Auth\UserInterface
{
DB::table('Accounts')->Where('username','=', $username)->get();
return new User;
}
/**
* Fetch the context by ID.
* @param string $id
* @return Auth\ContextInterface|null
*/
public function fetchContext(string $id) : ?Auth\ContextInterface
{
DB::table('Accounts')
->Where('id','=', $id)
->get();
return new Context();
}
/**
* Get the default context of the user.
* @param Auth\UserInterface $user
* @return Auth\ContextInterface|null
*/
public function getContextForUser(Auth\UserInterface $user) : ?Auth\ContextInterface
{
return null;
}
}