Class yii\authclient\AuthAction

Inheritanceyii\authclient\AuthAction » yii\base\Action
Available since version2.0

AuthAction performs authentication via different auth clients.

It supports yii\authclient\OpenId, yii\authclient\OAuth1 and yii\authclient\OAuth2 client types.

Usage:

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.

See also:

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$clientCollection string Name of the auth client collection application component. yii\authclient\AuthAction
$clientIdGetParamName string Name of the GET param, which is used to passed auth client id to this action. yii\authclient\AuthAction
$redirectView string Name or alias of the view file, which should be rendered in order to perform redirection. yii\authclient\AuthAction
$successCallback callable PHP callback, which should be triggered in case of successful authentication. yii\authclient\AuthAction

Public Methods

Hide inherited methods

MethodDescriptionDefined By
getCancelUrl() yii\authclient\AuthAction
getSuccessUrl() yii\authclient\AuthAction
redirect() Redirect to the given URL or simply close the popup window. yii\authclient\AuthAction
redirectCancel() Redirect to the \yii\authclient\cancelUrl or simply close the popup window. yii\authclient\AuthAction
redirectSuccess() Redirect to the URL. If URL is null, \yii\authclient\successUrl will be used. yii\authclient\AuthAction
run() Runs the action. yii\authclient\AuthAction
setCancelUrl() yii\authclient\AuthAction
setSuccessUrl() yii\authclient\AuthAction

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
auth() yii\authclient\AuthAction
authOAuth1() Performs OAuth1 auth flow. yii\authclient\AuthAction
authOAuth2() Performs OAuth2 auth flow. yii\authclient\AuthAction
authOpenId() Performs OpenID auth flow. yii\authclient\AuthAction
authSuccess() This method is invoked in case of successful authentication via auth client. yii\authclient\AuthAction
defaultCancelUrl() Creates default \yii\authclient\cancelUrl value. yii\authclient\AuthAction
defaultSuccessUrl() Creates default \yii\authclient\successUrl value. yii\authclient\AuthAction

Property Details

$clientCollection public property

Name of the auth client collection application component. It should point to yii\authclient\Collection instance.

public string $clientCollection 'authClientCollection'
$clientIdGetParamName public property

Name of the GET param, which is used to passed auth client id to this action. Note: watch for the naming, make sure you do not choose name used in some auth protocol.

public string $clientIdGetParamName 'authclient'
$redirectView public property

Name or alias of the view file, which should be rendered in order to perform redirection. If not set default one will be used.

public string $redirectView null
$successCallback public property

PHP callback, which should be triggered in case of successful authentication. This callback should accept yii\authclient\ClientInterface instance as an argument. For example:

public function onAuthSuccess($client)
{
    $attributes = $client->getUserAttributes();
    // user login or signup comes here
}

If this callback returns \yii\web\Response instance, it will be used as action response, otherwise redirection to \yii\authclient\successUrl will be performed.

public callable $successCallback null

Method Details

auth() protected method

protected \yii\web\Response auth ( $client )
$client mixed

Auth client instance.

return \yii\web\Response

Response instance.

throws \yii\base\NotSupportedException

on invalid client.

authOAuth1() protected method

Performs OAuth1 auth flow.

protected \yii\web\Response authOAuth1 ( $client )
$client yii\authclient\OAuth1

Auth client instance.

return \yii\web\Response

Action response.

authOAuth2() protected method

Performs OAuth2 auth flow.

protected \yii\web\Response authOAuth2 ( $client )
$client yii\authclient\OAuth2

Auth client instance.

return \yii\web\Response

Action response.

throws \yii\base\Exception

on failure.

authOpenId() protected method

Performs OpenID auth flow.

protected \yii\web\Response authOpenId ( $client )
$client yii\authclient\OpenId

Auth client instance.

return \yii\web\Response

Action response.

throws \yii\base\Exception

on failure.

throws \yii\web\HttpException

on failure.

authSuccess() protected method

This method is invoked in case of successful authentication via auth client.

protected \yii\web\Response authSuccess ( $client )
$client yii\authclient\ClientInterface

Auth client instance.

return \yii\web\Response

Response instance.

throws \yii\base\InvalidConfigException

on invalid success callback.

defaultCancelUrl() protected method

Creates default \yii\authclient\cancelUrl value.

protected string defaultCancelUrl ( )
return string

Cancel URL value.

defaultSuccessUrl() protected method

Creates default \yii\authclient\successUrl value.

protected string defaultSuccessUrl ( )
return string

Success URL value.

getCancelUrl() public method

public string getCancelUrl ( )
return string

Cancel URL.

getSuccessUrl() public method

public string getSuccessUrl ( )
return string

Successful URL.

redirect() public method

Redirect to the given URL or simply close the popup window.

public \yii\web\Response redirect ( $url, $enforceRedirect true )
$url mixed

URL to redirect, could be a string or array config to generate a valid URL.

$enforceRedirect boolean

Indicates if redirect should be performed even in case of popup window.

return \yii\web\Response

Response instance.

redirectCancel() public method

Redirect to the \yii\authclient\cancelUrl or simply close the popup window.

public \yii\web\Response redirectCancel ( $url null )
$url string

URL to redirect.

return \yii\web\Response

Response instance.

redirectSuccess() public method

Redirect to the URL. If URL is null, \yii\authclient\successUrl will be used.

public \yii\web\Response redirectSuccess ( $url null )
$url string

URL to redirect.

return \yii\web\Response

Response instance.

run() public method

Runs the action.

public void run ( )
setCancelUrl() public method

public void setCancelUrl ( $url )
$url string

Cancel URL.

setSuccessUrl() public method

public void setSuccessUrl ( $url )
$url string

Successful URL.