gelembjuk/auth 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gelembjuk/auth

Composer 安装命令:

composer require gelembjuk/auth

包简介

PHP Package for easy login to a web site with popular social networks. Now supports login with Facebook, Google, Twitter, LinkedIn.

README 文档

README

PHP Package for easy login to a web site with popular social networks.

Now supports login with Facebook, Google, Twitter, LinkedIn, Xing.

Installation

Using composer: gelembjuk/auth require: {"gelembjuk/auth": "*"}

Configuration

You need to get API keys for each of social networks.

Details on how to register application and get keys for each social network.

Twitter Register your application there https://apps.twitter.com/ . Copy Consumer Key and Secret Key in your application settings.

LinkedIn Go to https://developer.linkedin.com/, Then "My Apps". Create new App. Copy API Key and Secret. You need to set Authorized Redirect URLs. It will be an url to your "Login complete script".

Facebook Go to https://developers.facebook.com/. "Add new App" as Web-Page. Copy API Key and Secret. Enable "Web OAuth Login".

Google Go to https://code.google.com/apis/console. Create new Project. Enable Google+ API in this project in the APIs section. Then Create new Client ID in Credentials section. Choose "Web Application". Set up redirect url , it will be an url to your "Login complete script". Copy Client ID , Client Secret

NOTE. For some social networks it is needed to provide a correct login redirect url (callback url) in an application settings when you registering new keys. For example, you have to do this is Google API Console.

Login redirect url is a "complete login" url in your app. A script where you do final actions on a social login.

Recommended is to keep all integration options in one array in a separate safe configuration file. It is not required to have all integrations keys. Add only what you really need.

$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'
		)
	);

Usage

Start login process.

File 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;

Complete login process.

File 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();
}

Author

Roman Gelembjuk (@gelembjuk)

gelembjuk/auth 适用场景与选型建议

gelembjuk/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 698 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「twitter」 「google」 「facebook」 「linkedin」 「Social Login」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gelembjuk/auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gelembjuk/auth 我们能提供哪些服务?
定制开发 / 二次开发

基于 gelembjuk/auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 698
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 21
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-26