PHP code example of tomaszbrunarski / yii-eauth

1. Go to this page and download the library: Download tomaszbrunarski/yii-eauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomaszbrunarski / yii-eauth example snippets



...
	'import'=>array(
		'ext.eoauth.*',
		'ext.eoauth.lib.*',
		'ext.lightopenid.*',
		'ext.eauth.*',
		'ext.eauth.services.*',
	),
...
	'components'=>array(
		'loid' => array(
			'class' => 'ext.lightopenid.loid',
		),
		'eauth' => array(
			'class' => 'ext.eauth.EAuth',
			'popup' => true, // Use the popup window instead of redirecting.
			'cache' => false, // Cache component name or false to disable cache. Defaults to 'cache'.
			'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited.
			'services' => array( // You can change the providers and their classes.
				'google' => array(
					'class' => 'GoogleOpenIDService',
					//'realm' => '*.example.org',
				),
				'yandex' => array(
					'class' => 'YandexOpenIDService',
					//'realm' => '*.example.org',
				),
				'steam' => array(
					'class' => 'SteamOpenIDService',
					//'realm' => '*.example.org',
				),
				'yahoo' => array(
					'class' => 'YahooOpenIDService',
					//'realm' => '*.example.org',
				),
				'wargaming' => array(
					'class' => 'WargamingOpenIDService'
				),
				'twitter' => array(
					// register your app here: https://dev.twitter.com/apps/new
					'class' => 'TwitterOAuthService',
					'key' => '...',
					'secret' => '...',
				),
				'google_oauth' => array(
					// register your app here: https://code.google.com/apis/console/
					'class' => 'GoogleOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
					'title' => 'Google (OAuth)',
				),
				'yandex_oauth' => array(
					// register your app here: https://oauth.yandex.ru/client/my
					'class' => 'YandexOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
					'title' => 'Yandex (OAuth)',
				),
				'facebook' => array(
					// register your app here: https://developers.facebook.com/apps/
					'class' => 'FacebookOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'linkedin' => array(
					// register your app here: https://www.linkedin.com/secure/developer
					'class' => 'LinkedinOAuthService',
					'key' => '...',
					'secret' => '...',
				),
				'github' => array(
					// register your app here: https://github.com/settings/applications
					'class' => 'GitHubOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'live' => array(
					// register your app here: https://manage.dev.live.com/Applications/Index
					'class' => 'LiveOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'vkontakte' => array(
					// register your app here: https://vk.com/editapp?act=create&site=1
					'class' => 'VKontakteOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'mailru' => array(
					// register your app here: http://api.mail.ru/sites/my/add
					'class' => 'MailruOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'moikrug' => array(
					// register your app here: https://oauth.yandex.ru/client/my
					'class' => 'MoikrugOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'odnoklassniki' => array(
					// register your app here: http://dev.odnoklassniki.ru/wiki/pages/viewpage.action?pageId=13992188
					// ... or here: http://www.odnoklassniki.ru/dk?st.cmd=appsInfoMyDevList&st._aid=Apps_Info_MyDev
					'class' => 'OdnoklassnikiOAuthService',
					'client_id' => '...',
					'client_public' => '...',
					'client_secret' => '...',
					'title' => 'Odnokl.',
				),
				'dropbox' => array(
					// register your app here: https://www.dropbox.com/developers/apps/create
					'class' => 'DropboxOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'eve' => array(
					// register your app here: https://developers.eveonline.com/applications
					'class' => 'EveOnlineOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'slack' => array(
					// register your app here: https://api.slack.com/applications/new
					'class' => 'SlackOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
					'title' => 'Slack',
				),

			),
		),
		...
	),
...


...
	public function actionLogin() {
		$serviceName = Yii::app()->request->getQuery('service');
		if (isset($serviceName)) {
			/** @var $eauth EAuthServiceBase */
			$eauth = Yii::app()->eauth->getIdentity($serviceName);
			$eauth->redirectUrl = Yii::app()->user->returnUrl;
			$eauth->cancelUrl = $this->createAbsoluteUrl('site/login');

			try {
				if ($eauth->authenticate()) {
					//var_dump($eauth->getIsAuthenticated(), $eauth->getAttributes());
					$identity = new EAuthUserIdentity($eauth);

					// successful authentication
					if ($identity->authenticate()) {
						Yii::app()->user->login($identity);
						//var_dump($identity->id, $identity->name, Yii::app()->user->id);exit;

						// special redirect with closing popup window
						$eauth->redirect();
					}
					else {
						// close popup window and redirect to cancelUrl
						$eauth->cancel();
					}
				}

				// Something went wrong, redirect to login page
				$this->redirect(array('site/login'));
			}
			catch (EAuthException $e) {
				// save authentication error to session
				Yii::app()->user->setFlash('error', 'EAuthException: '.$e->getMessage());

				// close popup window and redirect to cancelUrl
				$eauth->redirect($eauth->getCancelUrl());
			}
		}

		// default authorization code through login/password ..
	}


	if (Yii::app()->user->hasFlash('error')) {
		echo '<div class="error">'.Yii::app()->user->getFlash('error').'</div>';
	}