PHP code example of unquam / nette-api-auth

1. Go to this page and download the library: Download unquam/nette-api-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/ */

    

unquam / nette-api-auth example snippets




declare(strict_types=1);

namespace App\Presentation\Api;

use Nette\Database\Explorer;
use Unquam\NetteApiAuth\ApiTokenService;
use Unquam\NetteApiAuth\BaseApiPresenter;
use Unquam\NetteApiAuth\RateLimiterService;
use Unquam\NetteApiAuth\RefreshTokenService;
use Unquam\NetteApiAuth\ScopeService;

class AuthPresenter extends BaseApiPresenter
{
    private Explorer $database;
    private RefreshTokenService $refreshTokenService;

    protected array $publicActions = ['login', 'refresh'];

    public function __construct(
        ApiTokenService $tokenService,
        ScopeService $scopeService,
        RateLimiterService $rateLimiter,
        RefreshTokenService $refreshTokenService,
        Explorer $database
    ) {
        parent::__construct($tokenService, $scopeService, $rateLimiter);
        $this->refreshTokenService = $refreshTokenService;
        $this->database            = $database;
    }

    // POST /api/auth/login
    public function actionLogin(): void
    {
        $this-> public function actionRefresh(): void
    {
        $this->



declare(strict_types=1);

namespace App\Presentation\Api;

use Nette\Database\Explorer;
use Unquam\NetteApiAuth\ApiTokenService;
use Unquam\NetteApiAuth\BaseApiPresenter;
use Unquam\NetteApiAuth\RateLimiterService;
use Unquam\NetteApiAuth\ScopeService;

class ArticlePresenter extends BaseApiPresenter
{
    private Explorer $database;

    protected array $publicActions = ['list', 'show'];

    public function __construct(
        ApiTokenService $tokenService,
        ScopeService $scopeService,
        RateLimiterService $rateLimiter,
        Explorer $database
    ) {
        parent::__construct($tokenService, $scopeService, $rateLimiter);
        $this->database = $database;
    }

    // GET /api/articles
    public function actionList(): void
    {
        $this->  $this->sendJson(['success' => true]);
    }
}

// generate a test token — prefix sk_test_
$tokenRaw = $this->tokenService->generate($user->id, 'web-app', false);

// generate a live token — prefix sk_live_
$tokenRaw = $this->tokenService->generate($user->id, 'web-app', true);

if ($this->isLiveMode()) {
    // token starts with sk_live_ — production mode
    $this->sendJson(['status' => 'charged', 'amount' => $data['amount']]);
} else {
    // token starts with sk_test_ — sandbox mode
    $this->sendJson(['status' => 'sandbox', 'amount' => $data['amount']]);
}

$tokenRaw = $this->tokenService->generate($userId, 'mobile-app', false, ['read', 'write']);

$this->>

// revoke a specific token using its raw value
$this->tokenService->revoke($raw);

// revoke a specific token by its database id, only the owner can revoke it
$this->tokenService->revokeById($id, $userId);

// revoke every access token belonging to a user
$this->tokenService->revokeAll($userId);

// revoke every refresh token belonging to a user
$this->refreshTokenService->revokeAll($userId);

// revoke all refresh tokens linked to a specific access token
$this->refreshTokenService->revokeByApiToken($apiTokenId);

$this->getCurrentUser()                   // returns the authenticated user data as an array
$this->getCurrentScopes()                 // returns the scopes assigned to the current token
$this->isLiveMode()                       // returns true when the request uses a live token
$this->$this->