PHP code example of bnviking / yii2-oauth2

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

    

bnviking / yii2-oauth2 example snippets


'components' => [
    ...
    'bnvOAuth2'=> [
        'class' => \bnviking\oauth2\OAuth2Config::class,
        'clientUrlName' => 'client',
        'authUrl' => 'auth2/authorize', 
        'clients' => [
            'discord'=>[
                'class'=> \bnviking\oauth2\clients\Discord::class,
                'clientID' => 'discord_client_id',
                'clientSecret' => 'discord_client_secret',
            ],
            'vkontakte'=>[
                'class'=> \bnviking\oauth2\clients\VKontakte::class,
                'clientID' => 'vkontakte_client_id',
                'clientSecret' => 'vkontakte_client_secret',
            ],
            'yandex'=>[
                'class'=> \bnviking\oauth2\clients\Yandex::class,
                'clientID' => 'yandex_client_id',
                'clientSecret' => 'yandex_client_secret',
            ],
            'mailru'=>[
                'class'=> \bnviking\oauth2\clients\MailRu::class,
                'clientID' => 'mailru_client_id',
                'clientSecret' => 'mailru_client_secret',
            ],
            'trovo'=>[
                'class'=> \bnviking\oauth2\clients\Trovo::class,
                'clientID' => 'trovo_client_id',
                'clientSecret' => 'trovo_client_secret',
            ],
            'twitch'=>[
                'class'=> \bnviking\oauth2\clients\Twitch::class,
                'clientID' => 'twitch_client_id',
                'clientSecret' => 'twitch_client_secret',
            ],
        ]
    ]
    ...
]




namespace app\controllers;

use bnviking\oauth2\OAuth2;
use yii\web\Controller;
use Yii;

class Auth2Controller extends Controller
{
    public function actionAuthorize(): string
    {
        /** @var \bnviking\oauth2\components\AuthResult $authResult */
        $authResult = OAuth2::init();

        if ($authResult->hasError()) {
            $errors = $authResult->getErrors();
            Yii::$app->session->setFlash('error', implode('<br>', $errors));
            $this->goHome();
            return '';
        }

        if ($authResult->getAction() === AuthResult::ACTION_ENTERS_SITE) {
            /** @var \bnviking\oauth2\components\OAuth2BaseClient $clientData Auth client data */
            $clientData = $authResult->getClient();
            /** @var \bnviking\oauth2\components\UserResult $userData User data */
            $userData = $authResult->getUser();
            /*
             * property $userData:
             *   id - User ID
             *   email - User email
             *   username - User name
             *   token - Auth token
             *   tokenReset - Reset token
             *   tokenType - Token Type
             *   tokenExpires - Token lifetime [seconds]
            */

            /*
             * Here you can register or enter the site
             */

        }
        ...
    }
}


<?=\bnviking\oauth2\widgets\OAuth2Buttons::widget()

'components' => [
    ...
     'bnvOAuth2'=> [
        'clients' => [
            ...
            'htmlOptions' => ['class'=>'my-css-class']
        ]
     ]
]


use bnviking\oauth2\components\ClientsManager;
use bnviking\oauth2\exception\OAuth2Exception;
use yii\base\Widget;

class OAuth2Buttons extends Widget
{
    public function run(): string
    {
        $error = '';
        $clients = [];
        try {
            $clientManager = new ClientsManager();
            /** @var \bnviking\oauth2\components\OAuth2BaseClient[] $clients Auth client data */
            $clients = $clientManager->getClients();
        } catch (OAuth2Exception $e) {
            $error = $e->getMessage();
        }

        return $this->render('view-widget',['clients' => $clients,'error' => $error]);
    }

}


/* @var yii\web\View $this */
/* @var OAuth2BaseClient[] $clients */
/* @var string $error */

use bnviking\oauth2\components\OAuth2BaseClient;
use bnviking\oauth2\exception\OAuth2Exception;
use yii\helpers\Html;

\bnviking\oauth2\OAuth2Bundle::register($this);

php composer.phar