About Yii2-Authcliens-Authaction

1.there is a document for setting the cancelUrl?

  1. I use the AuthAction for the code:

in cofig:




		'urlManager' => [

			'enablePrettyUrl' => true,

			'showScriptName' => false,

			'rules' => [

				'authlogin' =>'test/auth',

			],

		],

		'authClientCollection' => [

			'class' => 'yii\authclient\Collection',

			'clients' => [

				'weibo' => [

					'class' => 'app\api\clients\Weibo',

					'clientId' => 'xxxxxxx',

					'clientSecret' => 'xxxxxxx',

				],

			],

		],



in clients php :




<?php


namespace app\api\clients;


use yii\authclient\OAuth2;


class Weibo extends OAuth2{


	/**

	 * @inheritdoc

	 */

	public $authUrl = 'https://api.weibo.com/oauth2/authorize';

	/**

	 * @inheritdoc

	 */

	public $tokenUrl = 'https://api.weibo.com/oauth2/access_token';

	/**

	 * @inheritdoc

	 */

	public $apiBaseUrl = 'https://api.weibo.com';

	/**

	 * @inheritdoc

	 */

	public $scope = '';




	/**

	 * @inheritdoc

	 */

	protected function initUserAttributes()

	{

		return $this->api('users/show.json', 'GET');

	}

	/**

	 * @inheritdoc

	 */

	public function apiInternal($accessToken, $url, $method, array $params){

		$params ['uid'] = $accessToken->getParam('uid');

		$params['access_token'] = $accessToken->getToken();


		return $this->sendRequest($method, $url, $params);

	}


	/**

	 * @inheritdoc

	 */

	public function fetchAccessToken($authCode, array $params = [])

	{

		$token = parent::fetchAccessToken($authCode, $params = []);

		$token->setExpireDurationParamKey($token->getParam('expires_in'));

		$token->setToken($token->getParam('access_token'));


		return $token;

	}


	/**

	 * @inheritdoc

	 */

	protected function defaultName()

	{

		return 'weibo';

	}


	/**

	 * @inheritdoc

	 */

	protected function defaultTitle()

	{

		return 'weibo';

	}




	public function defaultNormalizeUserAttributeMap(){

		return  [

			'connect_id' => 'id',

			'name' => 'name',

			'screen_name'=>'screen_name',

			'avatar_large' => 'avatar_large',

			'avatar_small'=> 'profile_image_url',

			'location'=>'location',

			'description'=>'description',

			'url'=>'url',

			'weibo_status' =>'status',

		];

	}

}



in controller




<?php


namespace app\controllers;


use app\components\BaseController;

use app\models\User;

use app\models\Connect;


class TestController extends \yii\web\Controller

{

	public $layout = false;




	public function actions()

	{

		return [

			'auth' => [

				'class' => 'yii\authclient\AuthAction',

				'successCallback' => [$this, 'successCallback'],

			],

		];

    }


	public function actionIndex()

	{


		return $this->render('index');

	}




	public function successCallback($client)

	{

		$attributes = $client->getUserAttributes();

		$ac = $client->getAccessToken();

		$user = null;

		$connect = null;

		if($connect = Connect::findOne(['connect_id'=>$ac->getParam('uid'),'authclient'=>$client->name])){

			$user = User::findOne($connect->user_id);

		}else{

			$connect = new Connect();

			$user = new User();

		}


		$user->setAttributes($attributes);

		$user->status = 1;

		try{

			if($user->save()){

				$connect->user_id = $user->id;

				$connect->connect_id = $ac->getParam('uid');

				$connect->access_token = $ac->getParam('access_token');

				$connect->expires_in = $ac->getParam('expires_in')+$ac->createTimestamp;

				$connect->authclient = $client->name;

				$connect->save();

			}

		}catch (\Exception $e){

			\Yii::error('login:'.json_encode($attributes));

		}

	}

}



in view:




<?php use yii\authclient\widgets\AuthChoice; ?>


<?php $authAuthChoice = AuthChoice::begin([

    'baseAuthUrl' => ['test/auth']

]); ?>

<ul>

	<?php foreach ($authAuthChoice->getClients() as $client): ?>

		<li><?= $authAuthChoice->clientLink($client) ?></li>

	<?php endforeach; ?>

</ul>

<?php AuthChoice::end(); ?>



when the success function run compelte, I always redirect to the site/auth,I am not set the url rule;who can help me!

Seems pretty straightforward to me:

https://github.com/yiisoft/yii2/blob/master/extensions/authclient/AuthAction.php

I think the $_cancelUrl and $_successUrl may declare as public ,so we can set in controller whick import the actions of ‘auth’

Actually you can already do this. There’s no need for them to be public because they don’t get set directly. Instead, they’ll get set via public function setSuccessUrl($url) and public function setCancelUrl($url).

So this is all you need:




public function actions()

{

    return [

        'auth' => [

            'class' => 'yii\authclient\AuthAction',

            'successCallback' => [$this, 'successCallback'],

            'successUrl' => 'your/url'

            'cancelUrl' => 'your/url'

        ],

    ];

}




i got it,thanks;