texhub/social-auth

Simple, elegant social OAuth 2.0 login for any PHP framework with first-class Laravel support β€” Google & GitHub out of the box, easily extensible.

Maintainers

Package info

github.com/TexhubPro/social-auth

Homepage

Issues

pkg:composer/texhub/social-auth

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

v1.0.0 2026-06-01 17:06 UTC

This package is auto-updated.

Last update: 2026-06-01 17:10:18 UTC


README

🌐 English Β· Русский

License: MIT PHP Laravel

Simple, elegant social OAuth 2.0 login for any PHP framework β€” Google & GitHub out of the box, easily extensible β€” with first-class Laravel support.

✨ Features

  • πŸ” OAuth 2.0 Authorization Code flow done for you
  • 🟒 Google & πŸ™ GitHub providers built in (GitHub falls back to the verified primary email)
  • πŸ‘€ Normalized User β€” id, nickname, name, email, avatar, token, raw
  • πŸ›‘ CSRF state helper, custom scopes & extra params
  • 🧩 Extensible β€” add your own provider in a few lines
  • πŸ§ͺ Fully unit-tested (no network), pluggable HTTP transport

πŸ“¦ Installation

composer require texhub/social-auth

Requirements: PHP β‰₯ 8.2 with curl, json.

πŸš€ Quick start

use TexHub\SocialAuth\SocialAuth;

$social = SocialAuth::fromArray([
    'google' => [
        'client_id' => '...', 'client_secret' => '...',
        'redirect' => 'https://app.tj/auth/google/callback',
    ],
    'github' => [
        'client_id' => '...', 'client_secret' => '...',
        'redirect' => 'https://app.tj/auth/github/callback',
    ],
]);

// 1) Send the user to the provider (store the state in the session for CSRF):
$state = SocialAuth::generateState();
$_SESSION['oauth_state'] = $state;
header('Location: ' . $social->driver('google')->redirectUrl($state));

// 2) On your callback, verify state then get the user:
if (($_GET['state'] ?? null) !== ($_SESSION['oauth_state'] ?? null)) {
    exit('Invalid state');
}
$user = $social->driver('google')->userFromCode($_GET['code']);

$user->id;            // provider user id
$user->name;          // full name
$user->email;         // email
$user->avatar;        // avatar URL
$user->nickname;      // login/handle (GitHub)
$user->token->accessToken;   // OAuth access token
$user->token->refreshToken;  // when available (Google offline access)

🟒 Google / πŸ™ GitHub

Both work identically β€” just switch the driver name:

$social->driver('github')->redirectUrl($state);
$user = $social->driver('github')->userFromCode($code);

Default scopes: Google β†’ openid profile email, GitHub β†’ read:user user:email. Override per provider via scopes, and add provider params via extra (e.g. Google ['access_type' => 'offline', 'prompt' => 'consent'] for a refresh token).

🧩 Add your own provider

use TexHub\SocialAuth\Providers\AbstractProvider;
use TexHub\SocialAuth\User;

final class FacebookProvider extends AbstractProvider
{
    protected function authorizeUrl(): string { return 'https://www.facebook.com/v19.0/dialog/oauth'; }
    protected function tokenUrl(): string     { return 'https://graph.facebook.com/v19.0/oauth/access_token'; }
    protected function defaultScopes(): array { return ['email', 'public_profile']; }
    protected function fetchUser(string $token): array {
        return $this->get('https://graph.facebook.com/me?fields=id,name,email,picture', $token);
    }
    protected function mapUser(array $raw): User {
        return new User((string) $raw['id'], null, $raw['name'] ?? null, $raw['email'] ?? null,
            $raw['picture']['data']['url'] ?? null, $raw);
    }
}

$social->extend('facebook', FacebookProvider::class)
       ->configure('facebook', \TexHub\SocialAuth\ProviderConfig::fromArray($cfg));

βš™οΈ Error handling

use TexHub\SocialAuth\Exceptions\ApiException;
use TexHub\SocialAuth\Exceptions\ConfigurationException;

try {
    $user = $social->driver('google')->userFromCode($code);
} catch (ApiException $e) {
    // token exchange / profile error β€” $e->getMessage(), $e->httpStatus, $e->payload
} catch (ConfigurationException $e) {
    // unknown / unconfigured provider
}

🧩 Laravel

Auto-discovered. Publish config:

php artisan vendor:publish --tag=social-auth-config

.env:

GOOGLE_CLIENT_ID=...
GOOGLE_CLIENT_SECRET=...
GOOGLE_REDIRECT_URI=https://app.tj/auth/google/callback

GITHUB_CLIENT_ID=...
GITHUB_CLIENT_SECRET=...
GITHUB_REDIRECT_URI=https://app.tj/auth/github/callback

Controller:

use Illuminate\Http\Request;
use TexHub\SocialAuth\Laravel\SocialAuth;

public function redirect(string $provider, Request $request)
{
    $state = \TexHub\SocialAuth\SocialAuth::generateState();
    $request->session()->put('oauth_state', $state);

    return redirect()->away(SocialAuth::driver($provider)->redirectUrl($state));
}

public function callback(string $provider, Request $request)
{
    abort_unless($request->query('state') === $request->session()->pull('oauth_state'), 419);

    $user = SocialAuth::driver($provider)->userFromCode($request->query('code'));

    $account = User::updateOrCreate(
        ['provider' => $provider, 'provider_id' => $user->id],
        ['name' => $user->name, 'email' => $user->email, 'avatar' => $user->avatar],
    );
    auth()->login($account);

    return redirect('/dashboard');
}

Routes:

Route::get('/auth/{provider}', [AuthController::class, 'redirect']);
Route::get('/auth/{provider}/callback', [AuthController::class, 'callback']);

πŸ§ͺ Testing

Inject a fake transport β€” no network needed:

use TexHub\SocialAuth\SocialAuth;
use TexHub\SocialAuth\Tests\Support\FakeTransport;

$t = (new FakeTransport())
    ->on('oauth2.googleapis.com/token', ['access_token' => 't', 'token_type' => 'Bearer'])
    ->on('userinfo', ['sub' => '1', 'email' => 'a@b.c', 'name' => 'A']);

$social = SocialAuth::fromArray($configs, $t);
$user = $social->driver('google')->userFromCode('code');
composer install && composer test

πŸ“š Architecture

src/
β”œβ”€β”€ SocialAuth.php           # manager / driver factory
β”œβ”€β”€ ProviderConfig.php       # per-provider client id/secret/redirect/scopes
β”œβ”€β”€ Token.php Β· User.php     # normalized value objects
β”œβ”€β”€ Contracts/Provider.php   # provider interface
β”œβ”€β”€ Providers/               # AbstractProvider, GoogleProvider, GitHubProvider
β”œβ”€β”€ Http/                    # Transport, CurlTransport, RawResponse
β”œβ”€β”€ Exceptions/              # ApiException, ConfigurationException, …
└── Laravel/                 # ServiceProvider + Facade

License

MIT Β© TexHub Pro β€” built by Mahmudi Shodmehr.