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/ */
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
*/
}
...
}
}