ravindrasingh0406/social-login
Composer 安装命令:
composer require ravindrasingh0406/social-login
包简介
Framework-agnostic social login package for PHP with multiple providers (Google, GitHub)
README 文档
README
A tiny, framework-agnostic PHP library to handle OAuth2 social login with multiple providers. It uses league/oauth2-client under the hood and ships with 6 built-in providers plus full extensibility for any OAuth2 service.
✨ Features
- 🔐 6 Built-in Providers: Google, GitHub, Facebook, LinkedIn, Microsoft, Twitter/X
- 🏢 Custom OAuth2 Support: Use your own OAuth2 server as a provider
- 🔌 Fully Extensible: Add any OAuth2 provider in minutes
- 🚀 Framework-agnostic: Works with any PHP application
- 💎 Laravel Support: Auto-discovery, Facade, publishable config
- 🧪 Well-tested: PHPUnit test suite included
- 📦 Zero Config: Sensible defaults, customize as needed
Install
Require via Composer:
composer require ravindrasingh0406/social-login
Note: This package depends on
league/oauth2-clientandguzzlehttp/guzzle.
Laravel usage
This package supports Laravel Package Auto-Discovery.
- Install
composer require ravindrasingh0406/social-login
- Publish config (optional)
php artisan vendor:publish --tag=social-login-config
This creates config/social-login.php so you can set credentials or use env vars:
SOCIAL_GOOGLE_CLIENT_ID=...
SOCIAL_GOOGLE_CLIENT_SECRET=...
SOCIAL_GOOGLE_REDIRECT_URI=https://your-app.test/auth/callback?provider=google
SOCIAL_GITHUB_CLIENT_ID=...
SOCIAL_GITHUB_CLIENT_SECRET=...
SOCIAL_GITHUB_REDIRECT_URI=https://your-app.test/auth/callback?provider=github
- Use via Facade or DI
use SocialLogin\Laravel\Facades\SocialLogin; // Facade // or type-hint SocialLogin\Manager $social in constructors // Step 1: redirect $authUrl = SocialLogin::getAuthorizationUrl(); session(['oauth2state' => SocialLogin::getState()]); return redirect()->away($authUrl); // Step 2: callback abort_unless(request('state') === session('oauth2state'), 400, 'Invalid state'); $token = SocialLogin::fetchAccessToken(request('code')); $user = SocialLogin::fetchUser($token['access_token']);
To select a specific provider, resolve the Manager and call driver('google'|'github'):
use SocialLogin\Manager; public function redirectGoogle(Manager $social) { $driver = $social->driver('google'); return redirect()->away($driver->getAuthorizationUrl()); }
Quick start (generic PHP)
use SocialLogin\Manager; $config = [ 'providers' => [ 'google' => [ 'client_id' => 'GOOGLE_CLIENT_ID', 'client_secret' => 'GOOGLE_CLIENT_SECRET', 'redirect_uri' => 'https://your-app.test/callback?provider=google', // Optional: 'scopes' => ['openid','email','profile'], ], 'github' => [ 'client_id' => 'GITHUB_CLIENT_ID', 'client_secret' => 'GITHUB_CLIENT_SECRET', 'redirect_uri' => 'https://your-app.test/callback?provider=github', // Optional: 'scopes' => ['read:user','user:email'], ], ], ]; $manager = new Manager($config); $driver = $manager->driver('google'); // 1) Redirect user to provider $authUrl = $driver->getAuthorizationUrl(); $state = $driver->getState(); // persist this in session to validate later header('Location: '.$authUrl); exit; // 2) In callback handler $token = $driver->fetchAccessToken($_GET['code']); $user = $driver->fetchUser($token['access_token']);
Demo app
A tiny demo is available in examples/public:
- Copy
examples/public/config.sample.phptoexamples/public/config.phpand fill credentials - Serve the folder, e.g.:
php -S localhost:8000 -t examples/public
Then open http://localhost:8000 in your browser.
Supported Providers
| Provider | Name | Default Scopes |
|---|---|---|
google |
openid, email, profile |
|
| GitHub | github |
read:user, user:email |
facebook |
email, public_profile |
|
linkedin |
openid, profile, email |
|
| Microsoft | microsoft |
openid, profile, email, User.Read |
| Twitter/X | twitter |
tweet.read, users.read |
Want to add more? See EXTENDING.md for a complete guide on adding any OAuth2 provider.
Quick Example: Facebook
$config = [ 'providers' => [ 'facebook' => [ 'client_id' => 'YOUR_FACEBOOK_APP_ID', 'client_secret' => 'YOUR_FACEBOOK_SECRET', 'redirect_uri' => 'https://your-app.com/auth/facebook/callback', ], ], ]; $manager = new Manager($config); $driver = $manager->driver('facebook'); $authUrl = $driver->getAuthorizationUrl();
Extensibility
Adding Custom Providers
This package is designed to be fully extensible. Add any OAuth2 provider in 3 ways:
Method 1: Register globally (recommended)
use SocialLogin\Manager; use App\OAuth\DiscordProvider; Manager::extend('discord', DiscordProvider::class); $driver = $manager->driver('discord');
Method 2: Specify in config
$config = [ 'providers' => [ 'discord' => [ 'driver' => \App\OAuth\DiscordProvider::class, 'client_id' => '...', 'client_secret' => '...', 'redirect_uri' => '...', ], ], ];
Method 3: Create a provider class
use SocialLogin\Providers\AbstractOAuth2Provider; use SocialLogin\DTO\User; class DiscordProvider extends AbstractOAuth2Provider { protected function authorizeUrl(): string { return 'https://discord.com/api/oauth2/authorize'; } protected function tokenUrl(): string { return 'https://discord.com/api/oauth2/token'; } protected function defaultScopes(): array { return ['identify', 'email']; } protected function doFetchUser(string $accessToken): User { // Fetch and map user data $resp = $this->http->get('https://discord.com/api/users/@me', [ 'headers' => ['Authorization' => 'Bearer ' . $accessToken], ]); $data = json_decode((string) $resp->getBody(), true); return new User( id: $data['id'], name: $data['username'], email: $data['email'] ?? null, avatar: "https://cdn.discordapp.com/avatars/{$data['id']}/{$data['avatar']}.png", raw: $data, provider: 'discord' ); } }
📖 Full Guide: See EXTENDING.md for complete documentation, real-world examples (Slack, Apple, Discord), and testing strategies.
Using Your Own OAuth2 Server
You can use this package with your own custom OAuth2 authentication server - perfect for enterprise SSO, multi-tenant applications, or custom authentication services.
Quick Example
use SocialLogin\Providers\GenericOAuth2Provider; $config = [ 'providers' => [ 'my-auth-service' => [ 'driver' => GenericOAuth2Provider::class, 'client_id' => 'your_client_id', 'client_secret' => 'your_client_secret', 'redirect_uri' => 'https://your-app.com/auth/callback', // Your OAuth2 server endpoints 'authorize_url' => 'https://auth.yourcompany.com/oauth/authorize', 'token_url' => 'https://auth.yourcompany.com/oauth/token', 'userinfo_url' => 'https://auth.yourcompany.com/api/user', 'scopes' => ['openid', 'profile', 'email'], ], ], ]; $manager = new Manager($config); $driver = $manager->driver('my-auth-service');
Works With Popular OAuth2 Servers
- Laravel Passport - Full OAuth2 server for Laravel apps
- Keycloak - Open-source identity and access management
- Auth0 - Authentication and authorization platform
- Your Custom Server - Any OAuth2-compliant server
📖 Complete Guide: See CUSTOM_OAUTH2.md for:
- Building your own OAuth2 server
- Complete configuration options
- Field mapping and customization
- Laravel Passport, Keycloak, and Auth0 examples
- Security best practices
More Providers
More can be added by implementing SocialLogin\\Contracts\\ProviderDriver and extending Providers\\AbstractOAuth2Provider.
License
MIT
ravindrasingh0406/social-login 适用场景与选型建议
ravindrasingh0406/social-login 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「oauth2」 「github」 「google」 「laravel」 「social-login」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ravindrasingh0406/social-login 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ravindrasingh0406/social-login 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ravindrasingh0406/social-login 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Handles basic OAuth/OAuth2 authentication along with classes for common services
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
Email Toolkit Plugin for CakePHP
Laravel package for the simplification and integration of many of your users most wanted login providers. Installation is a breeze. Never worry about OAuth again.
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-06