PHP code example of heggi / yii2-esia
1. Go to this page and download the library: Download heggi/yii2-esia 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/ */
heggi / yii2-esia example snippets
return [
'components' => [
...
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
...
'esia' => [
'class' => 'heggi\yii2esia\Esia',
'clientId' => 'xxx',
'certPath' => __DIR__ . '/xxx.pem',
'privateKeyPath' => __DIR__ . '/xxx.key',
'privateKeyPassword' => 'xxx',
'scope' => 'fullname',
'production' => false,
],
...
],
],
...
]
]
public function actions() {
return [
...
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'ssoCallback'],
],
...
];
}
public function ssoCallback($client) {
$attributes = $client->getUserAttributes();
$oid = $attributes['oid'];
$user = User::findByOid($oid);
if($user) {
return Yii::$app->user->login($user);
}
$personInfo = $client->getPersonInfo($oid);
$user = new User();
$user->oid = $oid;
$user->first_name = $personInfo['firstName'];
$user->last_name = $personInfo['lastName'];
$user->middle_name = $personInfo['middleName'];
if(!$user->save()) {
throw new yii\web\ServerErrorHttpException('Внутренняя ошибка сервера');
}
Yii::$app->user->login($user);
}
<?= Html::a('Войти через госуслуги', ['site/auth', 'authclient' => 'esia'])
<?= yii\authclient\widgets\AuthChoice::widget([
'baseAuthUrl' => ['site/auth'],
'popupMode' => false,
])
php composer.phar