PHP包,用于轻松通过流行的社交网络登录网站。现在支持使用Facebook、Google、Twitter、LinkedIn登录。

1.2.0 2023-12-03 08:40 UTC

This package is not auto-updated.

Last update: 2024-09-22 12:09:55 UTC


README

PHP包,用于轻松通过流行的社交网络登录网站。

现在支持使用Facebook、Google、Twitter、LinkedIn、Xing登录。

安装

使用composer: gelembjuk/auth require: {"gelembjuk/auth": "*"}

配置

您需要为每个社交网络获取API密钥。

关于如何注册应用程序和获取每个社交网络的密钥的详细信息。

Twitter 在此处注册您的应用程序 https://apps.twitter.com/ 。复制消费者密钥和秘密密钥到您的应用程序设置中。

LinkedIn 访问 https://developer.linkedin.com/,然后“我的应用程序”。创建新的应用程序。复制API密钥和秘密。需要设置授权重定向URL。这将是一个指向您的“登录完成脚本”的URL。

Facebook 访问 https://developers.facebook.com/。“添加新应用程序”作为网页。复制API密钥和秘密。启用“Web OAuth登录”。

Google 访问 https://code.google.com/apis/console。创建新项目。在API部分中启用此项目中的Google+ API。然后在凭证部分中创建新的客户端ID。选择“Web应用程序”。设置重定向URL,这将是一个指向您的“登录完成脚本”的URL。复制客户端ID和客户端秘密。

注意。对于某些社交网络,在注册新密钥时,需要在应用程序设置中提供正确的登录重定向URL(回调URL)。例如,您必须在Google API控制台中这样做。

登录重定向URL是您的应用程序中的“完整登录”URL。在社交登录上进行最终操作的脚本。

建议将所有集成选项保存在一个数组中,并放在单独的安全配置文件中。不需要所有集成密钥。只添加您真正需要的。

$integrations = array(
	'facebook' => array(
		'api_key' => 'fake facebook api key',
		'secret_key' => 'fake facebook secret key'
		),
	'twitter' => array(
		'consumer_key' => 'fake twitter consumer key',
		'consumer_secret' => 'fake twitter consumer secret'
		),
	'linkedin' => array(
		'api_key' => 'fake linkedin api key',
		'api_secret' => 'fake linkedin api secret'
		),
	'google' => array(
		'application_name' => 'Your application name',
		'client_id' => 'fake google api client id',
		'client_secret' => 'fake google api client secret'
		),
	'xingapi' => array(
		'consumer_key' => 'fake xing consumer key',
		'consumer_secret' => 'fake xing counsumer secret'
		),
	'liveid' => array(
		'consumer_key' => 'fake live id consumer key',
		'consumer_secret' => 'fake live id consumer secret'
		)
	);

用法

开始登录过程。

文件startlogin.php

// composer autoloader
require '../vendor/autoload.php';

$socialnetwork = $_REQUEST['network'];  // this is one of: facebook, google, twitter, linkedin

// create social network login object. The second argument is array of API settings for a social network
$network = Gelembjuk\Auth\AuthFactory::getSocialLoginObject($socialnetwork,$integrations[$socialnetwork]);

$redirecturl = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/completelogin.php';

$socialauthurl = $network->getLoginStartUrl($redirecturl);
		
// remember the state. it will be used when complete a social login
$_SESSION['socialloginsate_'.$socialnetwork] = $network->serialize();

// this is optional. you can include a network name in your redirect url and then extract
$_SESSION['socialloginnetwork'] = $socialnetwork;

header("Location: $socialauthurl",true,301);
exit;

完成登录过程。

文件completelogin.php

// composer autoloader
require '../vendor/autoload.php';

$socialnetwork = $_SESSION['socialloginnetwork']; 

$network = Gelembjuk\Auth\AuthFactory::getSocialLoginObject($socialnetwork,$integrations[$socialnetwork]);

try {
	// read some input parameters needed to complete auth by this social network
	$arguments = array();
	
	foreach ($network->getFinalExtraInputs() as $key) {
		$arguments[$key] = $_REQUEST[$key];
	}
	
	// restore to a state before redirect
	$network->unSerialize($_SESSION['socialloginsate_'.$socialnetwork]);
			
	// get authorized user short social profile
	$profile = $network->completeLogin($arguments);
	
	// save user info to a session
	$_SESSION['user'] = $profile;
	
	// now user is loged in!
	
	//redirect user to home page
	header("Location: index.php",true,301);
	exit;
	
} catch (Exception $e) {
	echo "Somethign went wrong during the login process<br>";
	echo "Error is: ".$e->getMessage();
}

作者

Roman Gelembjuk (@gelembjuk)