devdot/churchtools-oauth2-client

OAuth2 Client for ChurchTools

v1.0.0 2025-01-02 17:07 UTC

This package is auto-updated.

Last update: 2025-03-07 11:24:34 UTC


README

OAuth2 Client for ChurchTools

Installation

composer require devdot/churchtools-oauth2-client

Basic Usage

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;
    }
}