PHP code example of devdot / churchtools-oauth2-client
1. Go to this page and download the library: Download devdot/churchtools-oauth2-client 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/ */
devdot / churchtools-oauth2-client example snippets
use Devdot\ChurchTools\OAuth2\Client\Provider\ChurchTools;
$provider = new ChurchTools([
'url' => 'https://YOUR-INSTANCE.church.tools',
'clientId' => 'Client-ID-From-OAUTH2-Setup',
'redirectUri' => 'The Redirect Uri you provided to CT',
]);
$code = $_GET['code'] ?? null;
if ($code === null) {
// redirect to OAuth Server
$redirect = $provider->getAuthorizationUrl();
header('Location: ' . $redirect);
exit;
}
else {
try {
// attempt to get access tokens
$tokens = $provider->getAccessTokenFromCode($code);
// get the user that was authenticated
$oauthUser = $provider->getResourceOwner($tokens);
// store to session or use for further validation
// ...
}
catch (League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
if ($e->getMessage() === 'invalid_grant') {
// code is not valid anymore, try again
header('Location: ' . $provider->getAuthorizationUrl());
exit;
}
throw $e;
}
}