It supports OpenId, OAuth1 and OAuth2 client types.
Usage:
php
class SiteController extends Controller
{
public function actions()
{
return [
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'successCallback'],
],
]
}
public function successCallback($client)
{
$attributes = $client->getUserAttributes();
user login or signup comes here
}
}
Usually authentication via external services is performed inside the popup window.
This action handles the redirection and closing of popup window correctly.
/** * Runs the action. */ public function run() { try { return parent::run(); } catch (Exception $ex) { Yii::$app->session->setFlash('error', $ex->getMessage()); //Yii::$app->session->setFlash('error', Yii::t('yee/auth', "Authentication error occured.")); return $this->redirectCancel(); } }
/** @inheritdoc */ protected function authSuccess($client) { if (!is_callable($this->successCallback)) { $this->successCallback = [$this, 'onAuthSuccess']; parent::authSuccess($client); } else { /** @var OAuth2 $client */ $this->onAuthSuccess($client); parent::authSuccess($client); } }
/** * @inheritdoc */ public function actions() { $actions = []; if ($this->getConfig('socialSettings', 'enabled', false)) { $actions['auth'] = ['class' => AuthAction::classname(), 'successCallback' => [$this, 'onAuthSuccess']]; } $captcha = $this->getConfig('registrationSettings', 'captcha', false); if ($captcha !== false) { $captcha = ArrayHelper::getValue($captcha, 'action', []); $actions['captcha'] = $captcha; } return $actions; }
/** * @inheritdoc */ public function run() { try { return parent::run(); } catch (\yii\authclient\InvalidResponseException $e) { Yii::$app->session->setFlash('warning', Yii::t('app', 'Invalid response got: {response}. Please try again.', ['response' => $e->getMessage()])); $this->controller->layout = $this->controller->module->postRegistrationLayout; return $this->controller->render($this->errorView); } catch (\Exception $e) { Yii::$app->session->setFlash('error', Yii::t('app', $e->getMessage())); $this->controller->layout = $this->controller->module->postRegistrationLayout; return $this->controller->render($this->errorView); } }
protected function authSuccess($client) { /** @var \yii\authclient\BaseClient $client */ $this->socialServiceId = SocialService::classNameToId($client->className()); if ($this->socialServiceId === 0) { throw new ServerErrorHttpException("SocialService unknown"); } // first find user service on this id $userAttributes = $client->getUserAttributes(); $this->serviceId = $userAttributes['id']; //! @todo Add caching here based on commonTag $this->userService = UserService::find()->where(['service_id' => $this->serviceId, 'social_service_id' => $this->socialServiceId])->one(); return parent::authSuccess($client); }
/** @inheritdoc */ public function actions() { return ['auth' => ['class' => AuthAction::className(), 'successCallback' => Yii::$app->user->isGuest ? [$this, 'authenticate'] : [$this, 'connect']]]; }
/** * @inheritdoc */ public function getCancelUrl() { return $this->_module->getSocialSetting('defaultCancelUrl', $this->userType, parent::getCancelUrl()); }
/** * @inheritdoc */ public function actions() { return ['captcha' => ArrayHelper::merge(['class' => $this->module->getClassName(Module::CLASS_CAPTCHA_ACTION)], $this->module->captchaConfig), 'client' => ['class' => AuthAction::className(), 'successCallback' => [$this, 'clientLogin']]]; }
public function actions() { return ['social' => ['class' => AuthAction::className(), 'successCallback' => [$this, 'social']]]; }
public function actions() { return ['auth' => ['class' => AuthAction::className(), 'successCallback' => [$this, 'onAuthSuccess']]]; }
/** * @inheritdoc */ public function authSuccess($client) { return parent::authSuccess($client); }
/** * @inheritdoc */ public function actions() { return ['auth' => ['class' => AuthAction::className(), 'successCallback' => [$this, 'authCallback'], 'successUrl' => Yii::$app->urlManager->createAbsoluteUrl(['/user/default/login'])]]; }