PHP code example of burnthebook / craft-oauth

1. Go to this page and download the library: Download burnthebook/craft-oauth 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/ */

    

burnthebook / craft-oauth example snippets



// /modules/CustomOauthProviders/YourProvider.php
namespace modules\oauth\providers;

use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Grant\AbstractGrant;
use modules\yourmodule\resourceowners\YourResourceOwner;

class YourProvider extends GenericProvider
{
    protected function createAccessToken(array $response, AbstractGrant $grant)
    {
        // If your OAuth server returns a non-standard token response, you can adjust it here
        return parent::createAccessToken($response, $grant);
    }

    protected function createResourceOwner(array $response, AccessToken $token)
    {
        // Define the fields your OAuth server sends back
        return new YourResourceOwner($response);
    }

    protected function fetchResourceOwnerDetails(AccessToken $token)
    {
        // If your server doesn't use a separate userinfo URL,
        // you can fetch details manually here.
        return $token->getValues();
    }
}


// /modules/CustomOauthProviders/YourResourceOwner.php

namespace modules\oauth\providers;

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

class YourResourceOwner implements ResourceOwnerInterface
{
    protected $response;

    public function __construct(array $response)
    {
        $this->response = $response;
    }

    public function getId()
    {
        return $this->response['id'] ?? null;
    }

    public function getEmail()
    {
        return $this->response['email'] ?? null;
    }

    public function getName()
    {
        return $this->response['name'] ?? null;
    }

    public function toArray()
    {
        return $this->response;
    }
}