You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB

7 years ago
<?php
namespace frontend\controllers\auth;
use common\auth\Identity;
use core\services\auth\NetworkService;
3 years ago
use DomainException;
7 years ago
use Yii;
use yii\authclient\ClientInterface;
3 years ago
use yii\base\Exception;
7 years ago
use yii\helpers\ArrayHelper;
use yii\authclient\AuthAction;
use yii\web\Controller;
7 years ago
class NetworkController extends Controller
7 years ago
{
3 years ago
private NetworkService $service;
7 years ago
public function __construct($id, $module, NetworkService $service, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
7 years ago
}
3 years ago
public function actions(): array
7 years ago
{
return [
'auth' => [
'class' => AuthAction::class,
'successCallback' => [$this, 'onAuthSuccess'],
],
];
}
3 years ago
/**
* @param ClientInterface $client
* @throws Exception
* @throws \Exception
*/
7 years ago
public function onAuthSuccess(ClientInterface $client): void
{
$network = $client->getId();
$attributes = $client->getUserAttributes();
$identity = ArrayHelper::getValue($attributes, 'id');
try {
3 years ago
$user = $this->service->auth($network, $identity);
7 years ago
Yii::$app->user->login(new Identity($user), Yii::$app->params['user.rememberMeDuration']);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
}