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.
47 lines
1.3 KiB
47 lines
1.3 KiB
<?php |
|
|
|
namespace frontend\controllers\auth; |
|
|
|
use common\auth\Identity; |
|
use core\services\auth\NetworkService; |
|
use frontend\components\FrontendController; |
|
use Yii; |
|
use yii\authclient\ClientInterface; |
|
use yii\helpers\ArrayHelper; |
|
use yii\authclient\AuthAction; |
|
|
|
class NetworkController extends FrontendController |
|
{ |
|
private $service; |
|
|
|
public function __construct($id, $module, NetworkService $service, $config = []) |
|
{ |
|
parent::__construct($id, $module, $config); |
|
$this->service = $service; |
|
} |
|
|
|
public function actions() |
|
{ |
|
return [ |
|
'auth' => [ |
|
'class' => AuthAction::class, |
|
'successCallback' => [$this, 'onAuthSuccess'], |
|
], |
|
]; |
|
} |
|
|
|
public function onAuthSuccess(ClientInterface $client): void |
|
{ |
|
$network = $client->getId(); |
|
$attributes = $client->getUserAttributes(); |
|
$identity = ArrayHelper::getValue($attributes, 'id'); |
|
|
|
try { |
|
$user = $this->service->auth($network, $identity); |
|
Yii::$app->user->login(new Identity($user), Yii::$app->params['user.rememberMeDuration']); |
|
} catch (\DomainException $e) { |
|
Yii::$app->errorHandler->logException($e); |
|
Yii::$app->session->setFlash('error', $e->getMessage()); |
|
} |
|
} |
|
} |