PHP code example of chani / safi-auth

1. Go to this page and download the library: Download chani/safi-auth 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/ */

    

chani / safi-auth example snippets


use Safi\Extensions\Auth\AuthServiceProvider;

$provider = new AuthServiceProvider();
$provider->register($assembler);

use Safi\Extensions\Auth\AuthMiddleware;

$kernel = new Kernel(
    router: $router,
    logger: $logger,
    view: $viewEngine,
    middlewares: [
        $assembler->get(AuthMiddleware::class),
    ],
);

declare(strict_types=1);

namespace Components\Example\Controllers;

use Safi\Core\AbstractController;
use Safi\Core\Attributes\Route;
use Safi\Core\Http\Response;

final class ExampleController extends AbstractController
{
    #[Route('/public-page', method: 'GET', name: 'example.public', public: true)]
    public function publicPage(): Response
    {
        return $this->render('@Example/public');
    }

    #[Route('/protected-page', method: 'GET', name: 'example.protected')]
    public function protectedPage(): Response
    {
        return $this->render('@Example/protected');
    }
}

// 1. Verify TOTP Challenge Code
if ($authService->isTwoFactorPending()) {
    $success = $authService->verifyTwoFactorCode($userInputCode);
}

// 2. Generate Provisioning URI for QR Codes (e.g. Authenticator Apps)
$provisioningUri = $totpService->getProvisioningUri($user->email, $secret);