PHP code example of kodhe / socialite
1. Go to this page and download the library: Download kodhe/socialite 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/ */
kodhe / socialite example snippets
'callback_uri' => 'https://example.com/auth/socialite/callback',
'providers' => [
'google' => [
'client_id' => getenv('GOOGLE_CLIENT_ID'),
'client_secret' => getenv('GOOGLE_CLIENT_SECRET'),
'scopes' => ['openid', 'profile', 'email'],
],
// github, facebook, gitlab, linkedin, twitter ...
],
// GET /auth/socialite/{provider}
Route::get('/auth/socialite/{provider}', function ($provider) {
return socialite($provider)->redirect(); // helper global
// atau: return \Kodhe\Framework\Socialite\Facade::driver($provider)->redirect();
});
// GET /auth/socialite/callback/{provider}
Route::get('/auth/socialite/callback/{provider}', function ($provider) {
$user = socialite($provider)->user(); // tukar code -> token -> profil
// $user->getId(), getName(), getEmail(), getNickname(), getAvatar(),
// $user->getAccessToken(), getRefreshToken(), getExpiresIn(), getExtra()
// TODO: find-or-create user lokal lalu login
});
socialite('google')->scopes(['openid', 'email'])->redirect();
socialite('github')->with(['login' => 'octocat'])->redirect();
// Refresh token (OAuth 2)
$new = socialite('google')->refreshAccessToken($user->getRefreshToken());
// Provider custom (mis. Strava)
socialite()->extend('strava', fn (array $c) => new class('strava', $c) extends \Kodhe\Framework\Socialite\Two\AbstractProvider2 {
public function getAuthUrl(): string { return 'https://www.strava.com/oauth/authorize'; }
public function getTokenUrl(): string { return 'https://www.strava.com/api/v3/oauth/token'; }
public function getUserFromToken(string $t): \Kodhe\Framework\Socialite\SocialiteUser {
return $this->fillUser(new \Kodhe\Framework\Socialite\SocialiteUser(),
$this->httpGetForToken('https://www.strava.com/api/v3/athlete', $t),
['id' => ['id'], 'name' => ['firstname'], 'avatar' => ['profile']]);
}
public function authorizationUrl(): string { return $this->buildAuthQuery(); }
});
bash
php socialite/tests/run.php # tanpa phpunit/composer
# atau via PHPUnit standar pada composer test