PHP code example of carry0987 / falcon

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

    

carry0987 / falcon example snippets


$config = [
    'providers' => [
        'github' => [
            'client_id' => 'your_github_client_id',
            'client_secret' => 'your_github_client_secret',
            'redirect_uri' => 'https://your-website.com/path/to/callback.php?provider=github'
        ],
        'line' => [
            'client_id' => 'your_line_client_id', // Channel ID
            'client_secret' => 'your_line_client_secret', // Channel secret
            'redirect_uri' => 'https://your-website.com/path/to/callback.php?provider=line'
        ],
        'telegram' => [
            'client_id' => 'your_telegram_client_id', // Bot username
            'client_secret' => 'your_telegram_client_secret', // Bot token
            'redirect_uri' => 'https://your-website.com/path/to/callback.php?provider=telegram'
        ],
        // Configuration for other third-party login providers...
    ],
];

$falcon = new \carry0987\Falcon\Falcon($config);
$providerName = $_GET['provider'] ?? 'default';
$provider = $falcon->createProvider($providerName);

// Start the OAuth login process
if (!isset($_GET['code'])) {
    $loginUrl = $provider->authorize();
    // Redirect user to the login page
    header('Location: ' . $loginUrl);
    exit;
}

// Handle the callback and retrieve user information
if ($providerName === 'telegram') {
    // Special handling for Telegram login flow...
} else {
    $accessToken = $provider->getTokenWithAuthCode($_GET['code']);
    $user = $provider->getUser();
    
    // Output user information
    echo "<pre>" . print_r($user, true) . "</pre>";
}

if (isset($_GET['logout'])) {
    $provider->revokeAccessToken($_GET['access_token'] ?? null);
    // Redirect back to the login page or homepage
    header('Location: ?provider=' . $providerName);
    exit;
}