forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yii\authclient\clients\GooglePlus
extracted
- Loading branch information
1 parent
09985e1
commit b199a71
Showing
2 changed files
with
91 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* @link http://www.yiiframework.com/ | ||
* @copyright Copyright (c) 2008 Yii Software LLC | ||
* @license http://www.yiiframework.com/license/ | ||
*/ | ||
|
||
namespace yii\authclient\clients; | ||
|
||
/** | ||
* GooglePlus is an enhanced version of the [[GoogleOAuth]], which uses Google+ sign-in flow, | ||
* which relies on embedded JavaScript code to generate a sign-in button. | ||
* | ||
* Example application configuration: | ||
* | ||
* ~~~ | ||
* 'components' => [ | ||
* 'authClientCollection' => [ | ||
* 'class' => 'yii\authclient\Collection', | ||
* 'clients' => [ | ||
* 'google' => [ | ||
* 'class' => 'yii\authclient\clients\GooglePlus', | ||
* 'clientId' => 'google_client_id', | ||
* 'clientSecret' => 'google_client_secret', | ||
* ], | ||
* ], | ||
* ] | ||
* ... | ||
* ] | ||
* ~~~ | ||
* | ||
* @see GoogleOAuth | ||
* @see yii\authclient\widgets\GooglePlusButton | ||
* @see https://developers.google.com/+/web/signin | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 2.0 | ||
*/ | ||
class GooglePlus extends GoogleOAuth | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function defaultReturnUrl() | ||
{ | ||
return 'postmessage'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function defaultViewOptions() | ||
{ | ||
return [ | ||
'widget' => [ | ||
'class' => 'yii\authclient\widgets\GooglePlusButton' | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,29 +7,29 @@ | |
|
||
namespace yii\authclient\widgets; | ||
|
||
use yii\authclient\clients\GoogleOAuth; | ||
use yii\authclient\clients\GooglePlus; | ||
use yii\base\InvalidConfigException; | ||
use yii\base\Widget; | ||
use yii\helpers\Html; | ||
use yii\helpers\Url; | ||
use yii\web\View; | ||
|
||
/** | ||
* GoogleSignInButton renders Google+ sign-in button. | ||
* This widget is designed to interact with [[GoogleOAuth]]. | ||
* GooglePlusButton renders Google+ sign-in button. | ||
* This widget is designed to interact with [[GooglePlus]]. | ||
* | ||
* @see GoogleOAuth | ||
* @see GooglePlus | ||
* @see https://developers.google.com/+/web/signin/ | ||
* | ||
* @property string|array $callback | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 2.0 | ||
*/ | ||
class GoogleSignInButton extends Widget | ||
class GooglePlusButton extends Widget | ||
{ | ||
/** | ||
* @var GoogleOAuth google auth client instance. | ||
* @var GooglePlus google auth client instance. | ||
*/ | ||
public $client; | ||
/** | ||
|
@@ -72,8 +72,8 @@ public function getCallback() | |
*/ | ||
public function init() | ||
{ | ||
if (!($this->client instanceof GoogleOAuth)) { | ||
throw new InvalidConfigException('"' . $this->className() . '::client" must be instance of "' . GoogleOAuth::className() . '"'); | ||
if (!($this->client instanceof GooglePlus)) { | ||
throw new InvalidConfigException('"' . $this->className() . '::client" must be instance of "' . GooglePlus::className() . '"'); | ||
} | ||
} | ||
|
||
|
@@ -86,7 +86,12 @@ public function run() | |
return $this->renderButton(); | ||
} | ||
|
||
protected function generateCallback($url = null) | ||
/** | ||
* Generates JavaScript callback function, which will be used to handle auth response. | ||
* @param array $url auth callback URL. | ||
* @return string JavaScript function name. | ||
*/ | ||
protected function generateCallback($url = []) | ||
{ | ||
if (empty($url)) { | ||
$url = ['auth', 'authclient' => $this->client->id]; | ||
|
@@ -102,9 +107,21 @@ protected function generateCallback($url = null) | |
$js = <<<JS | ||
function $callbackName(authResult) { | ||
var urlParams = []; | ||
for (var propName in authResult) { | ||
urlParams.push(encodeURIComponent(propName) + '=' + encodeURIComponent(authResult[propName])); | ||
if (authResult['code']) { | ||
urlParams.push('code=' + encodeURIComponent(authResult['code'])); | ||
} else if (authResult['error']) { | ||
urlParams.push('error=' + encodeURIComponent(authResult['error'])); | ||
urlParams.push('error_description=' + encodeURIComponent(authResult['error_description'])); | ||
} else { | ||
for (var propName in authResult) { | ||
var propValue = authResult[propName]; | ||
if (typeof propValue != 'object') { | ||
urlParams.push(encodeURIComponent(propName) + '=' + encodeURIComponent(propValue)); | ||
} | ||
} | ||
} | ||
window.location = '$url' + urlParams.join('&'); | ||
} | ||
JS; | ||
|
@@ -142,6 +159,9 @@ protected function renderButton() | |
'data-cookiepolicy' => 'single_host_origin', | ||
'data-requestvisibleactions' => null, | ||
'data-scope' => $this->client->scope, | ||
'data-accesstype' => 'offline', | ||
'data-width' => 'iconOnly', | ||
//'data-approvalprompt' => 'force', | ||
], | ||
$this->buttonHtmlOptions | ||
); | ||
|