PHP code example of artisanpack-ui / microsoft-oauth

1. Go to this page and download the library: Download artisanpack-ui/microsoft-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/ */

    

artisanpack-ui / microsoft-oauth example snippets


app( \ArtisanPackUI\MicrosoftOAuth\Contracts\ConfigurationRepository::class )->save( [
    'client_id'     => 'your-application-client-id',
    'client_secret' => 'your-client-secret',
    'tenant'        => 'common',
] );

use ArtisanPackUI\Hooks\Facades\Filter;

// In your service provider's boot() method:
Filter::add( 'ap.microsoft.oauth.scopes', function ( array $scopes ): array {
    $scopes[] = 'https://www.bingapis.com/api/v7/businesses.readwrite';
    return $scopes;
} );

use ArtisanPackUI\MicrosoftOAuth\OAuth\OAuthManager;
use ArtisanPackUI\MicrosoftOAuth\OAuth\IncrementalConsentResult;

$result = app( OAuthManager::class )->incrementalAuthorizationUrl( $user->id );

return match ( true ) {
    IncrementalConsentResult::NoConnection === $result      => redirect()->route( 'microsoft.auth.connect' ),
    IncrementalConsentResult::AlreadyAuthorized === $result => back()->with( 'status', 'Already authorized.' ),
    default                                                 => redirect()->away( $result ),
};

use ArtisanPackUI\MicrosoftOAuth\Facades\MicrosoftOAuth;

$response = MicrosoftOAuth::request( $user->id )
    ->acceptJson()
    ->get( 'https://graph.microsoft.com/v1.0/me' );

use ArtisanPackUI\MicrosoftOAuth\Contracts\TokenProvider;

public function __construct( protected TokenProvider $tokens ) {}

public function fetchProfile( int $userId ): array
{
    return Http::withToken( $this->tokens->accessTokenFor( $userId ) )
        ->acceptJson()
        ->get( 'https://graph.microsoft.com/v1.0/me' )
        ->json();
}
bash
php artisan vendor:publish --tag=microsoft-oauth-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=microsoft-oauth-config