PHP code example of midorikocak / nanoauth

1. Go to this page and download the library: Download midorikocak/nanoauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

midorikocak / nanoauth example snippets




declare(strict_types=1);

namespace midorikocak\nanoauth;

interface AuthenticationInterface
{
    public function login(string $username, string $password): bool;

    public function logout(): void;

    public function isLogged(): bool;

    public function getLoggedUser();
}

$db = new Database(new PDO('sqlite::memory:'));
$userRepository = new UserRepository($db);
$auth = new Authentication($userRepository);



declare(strict_types=1);

namespace midorikocak\nanoauth;

interface UserInterface
{
    public function __construct(?string $id, string $username, string $email, string $password);

    public function getPassword(): string;

    public function getUsername(): string;

    public function getEmail(): string;

    public function getId(): ?string;

    public function setId(string $id): void;
}

$userRepository = new UserRepository($db);
$auth = new Authentication($userRepository);



declare(strict_types=1);

use midorikocak\nanodb\Database;
use midorikocak\nanoauth\UserRepository;
use midorikocak\nanoauth\Authentication;

$db = new Database(new PDO('sqlite::memory:'));

$userRepository = new UserRepository($db);

$auth = new Authentication($userRepository);
$entryRepository = new Journal($db);
$app = new App($entryRepository);

$this->app->setAuthentication($this->auth);


 

declare(strict_types=1);

use midorikocak\nanodb\Database;
use midorikocak\nanoauth\UserRepository;
use midorikocak\nanoauth\Authentication;
use midorikocak\nanoauth\AuthorizationTrait;

 class App
{
    use AuthorizationTrait;

    private Journal $journal;

    public function __construct(Journal $journal)
    {
        $this->journal = $journal;
    }

    public function addEntry(string $content)
    {
        $this->checkLogin();

        $entry = new Entry($content);
        $this->journal->save($entry);
    }
}