Class yii\authclient\AuthAction

Inheritanceyii\authclient\AuthAction » yii\base\Action » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2-authclient/blob/master/AuthAction.php

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
$behaviors yii\base\Behavior[] List of behaviors attached to this component. This property is read-only. yii\base\Component
$cancelCallback callable PHP callback, which should be triggered in case of authentication cancelation. yii\authclient\AuthAction
$cancelUrl string Cancel URL. yii\authclient\AuthAction
$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
$controller yii\base\Controller|yii\web\Controller|yii\console\Controller The controller that owns this action yii\base\Action
$id string ID of the action yii\base\Action
$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
$successUrl string Successful URL. yii\authclient\AuthAction
$uniqueId string The unique ID of this action among the whole application. This property is read-only. yii\base\Action
$user yii\web\User|array|string The User object or the application component ID of the user component. yii\authclient\AuthAction

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Action
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getCancelUrl() yii\authclient\AuthAction
getSuccessUrl() yii\authclient\AuthAction
getUniqueId() Returns the unique ID of this action among the whole application. yii\base\Action
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\authclient\AuthAction
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
redirect() Redirect to the given URL or simply close the popup window. yii\authclient\AuthAction
redirectCancel() Redirect to the $cancelUrl or simply close the popup window. yii\authclient\AuthAction
redirectSuccess() Redirect to the URL. If URL is null, $successUrl will be used. yii\authclient\AuthAction
run() Runs the action. yii\authclient\AuthAction
runWithParams() Runs this action with the specified parameters. yii\base\Action
setCancelUrl() yii\authclient\AuthAction
setSuccessUrl() yii\authclient\AuthAction
trigger() Triggers an event. yii\base\Component

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
afterRun() This method is called right after run() is executed. yii\base\Action
auth() Perform authentication for the given client. yii\authclient\AuthAction
authCancel() This method is invoked in case of authentication cancelation. 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
beforeRun() This method is called right before run() is executed. yii\base\Action
defaultCancelUrl() Creates default $cancelUrl value. yii\authclient\AuthAction
defaultSuccessUrl() Creates default $successUrl value. yii\authclient\AuthAction

Property Details

$cancelCallback public property (available since version 2.1.5)

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

public function onAuthCancel(ClientInterface $client)
{
    // set flash, logging, etc.
}

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

public callable $cancelCallback null
$cancelUrl public property

Cancel URL.

public string getCancelUrl ( )
public void setCancelUrl ( $url )
$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(ClientInterface $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 $successUrl will be performed.

public callable $successCallback null
$successUrl public property

Successful URL.

public string getSuccessUrl ( )
public void setSuccessUrl ( $url )
$user public property (available since version 2.1.8)

The User object or the application component ID of the user component.

public yii\web\User|array|string $user 'user'

Method Details

auth() protected method

Perform authentication for the given client.

protected yii\web\Response auth ( $client, $authUrlParams = [] )
$client mixed

Auth client instance.

$authUrlParams array

Additional auth GET params.

return yii\web\Response

Response instance.

throws yii\base\NotSupportedException

on invalid client.

authCancel() protected method (available since version 2.1.5)

This method is invoked in case of authentication cancelation.

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

Auth client instance.

return yii\web\Response

Response instance.

authOAuth1() protected method

Performs OAuth1 auth flow.

protected yii\web\Response authOAuth1 ( $client, $authUrlParams = [] )
$client yii\authclient\OAuth1

Auth client instance.

$authUrlParams array

Additional auth GET params.

return yii\web\Response

Action response.

authOAuth2() protected method

Performs OAuth2 auth flow.

protected yii\web\Response authOAuth2 ( $client, $authUrlParams = [] )
$client yii\authclient\OAuth2

Auth client instance.

$authUrlParams array

Additional auth GET params.

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 $cancelUrl value.

protected string defaultCancelUrl ( )
return string

Cancel URL value.

defaultSuccessUrl() protected method

Creates default $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.

init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )
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 $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, $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.