PHP code example of foxdeveloper / oauth2-gitea

1. Go to this page and download the library: Download foxdeveloper/oauth2-gitea 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/ */

    

foxdeveloper / oauth2-gitea example snippets


use FoxDeveloper\OAuth2\Client\Provider\Gitea;
use FoxDeveloper\OAuth2\Client\Provider\GiteaResourceOwner;

$provider = new Gitea([
    'clientId' => '{GITEA_CLIENT_ID}',
    'clientSecret' => '{GITEA_CLIENT_SECRET}',
    'redirectUri' => 'https://example.com/callback-url',
    'baseUrl' => '{GITEA_BASE_URL}',
]);

if (!isset($_GET['code'])) {

    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit();

} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code'],
    ]);

    try {
        /** @var GiteaResourceOwner */
        $user = $provider->getResourceOwner($token);

        printf('Hello %s!', $user->getLogin());
    } catch (\Exception $e) {
        exit('Oh dear...');
    }

    echo $token->getToken();
}