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.
54 lines
1.4 KiB
54 lines
1.4 KiB
<?php |
|
|
|
namespace frontend\controllers\auth; |
|
|
|
use common\auth\Identity; |
|
use core\services\auth\NetworkService; |
|
use DomainException; |
|
use Yii; |
|
use yii\authclient\ClientInterface; |
|
use yii\base\Exception; |
|
use yii\helpers\ArrayHelper; |
|
use yii\authclient\AuthAction; |
|
use yii\web\Controller; |
|
|
|
class NetworkController extends Controller |
|
{ |
|
private NetworkService $service; |
|
|
|
public function __construct($id, $module, NetworkService $service, $config = []) |
|
{ |
|
parent::__construct($id, $module, $config); |
|
$this->service = $service; |
|
} |
|
|
|
public function actions(): array |
|
{ |
|
return [ |
|
'auth' => [ |
|
'class' => AuthAction::class, |
|
'successCallback' => [$this, 'onAuthSuccess'], |
|
], |
|
]; |
|
} |
|
|
|
/** |
|
* @param ClientInterface $client |
|
* @throws Exception |
|
* @throws \Exception |
|
*/ |
|
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()); |
|
} |
|
} |
|
}
|
|
|