Skip to content

Commit

Permalink
refactor: simplify user authentication
Browse files Browse the repository at this point in the history
Improve usage of dependency injection container.

Use UserAuth::authenticated() instead of checking session variable.
  • Loading branch information
dfranco committed Dec 29, 2023
1 parent f9e797e commit df9a01c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 1 addition & 4 deletions application/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ public function login(Request $request, Response $response): Response

$this->session->set('user_authenticated', $this->userAuth->authUser($form_data['username'], $form_data['password']));

// TODO: fix $userAuth->authenticated()
//if ($this->userAuth->authenticated()) {

if ($this->session->get('user_authenticated') === 'yes') {
if ($this->userAuth->authenticated()) {

$username = Sanitizer::sanitize($form_data['username']);

Expand Down
7 changes: 6 additions & 1 deletion application/Middleware/DbAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use App\Libs\FileConfig;
use Core\App\UserAuth;
use Core\Exception\AppException;
use Core\Exception\ConfigFileException;
use GuzzleHttp\Psr7\Response;
use Odan\Session\SessionInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -43,11 +44,15 @@ class DbAuthMiddleware implements MiddlewareInterface
private ?string $basePath;

/**
* @param UserAuth $userAuth
* @param SessionInterface $session
* @param Twig $twig
* @throws AppException
* @throws ConfigFileException
*/
public function __construct(UserAuth $userAuth, SessionInterface $session, Twig $twig)
{
$this->dbAuth = new $userAuth;
$this->dbAuth = $userAuth;

// Check if database exists and is writable
$this->dbAuth->check();
Expand Down
4 changes: 4 additions & 0 deletions application/config/container-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Tables\JobTable;
use App\Tables\LogTable;
use App\Tables\PoolTable;
use App\Tables\UserTable;
use App\Tables\VolumeTable;
use Core\Db\DatabaseFactory;
use Odan\Session\PhpSession;
Expand Down Expand Up @@ -72,6 +73,9 @@
CatalogTable::class => function (SessionInterface $session) {
return new CatalogTable(DatabaseFactory::getDatabase($session->get('catalog_id', 0)));
},
UserTable::class => function () {
return new UserTable(DatabaseFactory::getDatabase());
},
LogTable::class => function (SessionInterface $session) {
return new LogTable(DatabaseFactory::getDatabase($session->get('catalog_id', 0)));
},
Expand Down
12 changes: 5 additions & 7 deletions core/App/UserAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class UserAuth
* @var UserTable
*/
private UserTable $userTable;
private SessionInterface $session;

/**
* @throws Exception
*/
public function __construct()
public function __construct(UserTable $userTable, SessionInterface $session)
{
$this->userTable = new UserTable(
DatabaseFactory::getDatabase()
);
$this->userTable = $userTable;
$this->session = $session;
}

/**
Expand Down Expand Up @@ -145,9 +145,7 @@ public function destroySession(Session\SessionInterface $session): void
*/
public function authenticated(): bool
{
$session = new Session\PhpSession();

if ($session->get('user_authenticated') === 'yes') {
if ($this->session->get('user_authenticated') === 'yes') {
return true;
}

Expand Down

0 comments on commit df9a01c

Please sign in to comment.