1. Go to this page and download the library: Download texhub/social-auth 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/ */
texhub / social-auth example snippets
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)