PHP code example of hanbz / passport-client

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

    

hanbz / passport-client example snippets


use App\Http\Controllers\Auth\OAuthController;
use hanbz\PassportClient\Facades\PassportClient;

Route::get('oauth/login', fn() => PassportClient::driver('passport')->redirect())->name('oauth.login');
Route::get('oauth/callback', [OAuthController::class, 'OAuthCallback']);

use App\Models\User;
use hanbz\PassportClient\Facades\PassportClient;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

    public function OAuthCallback()
    {
        $oauth_user = PassportClient::driver('passport')->user();

        $user = User::where('email', $oauth_user->getEmail())->first();

        if (is_null($user)) {
            $name = $oauth_user->getName();
            $email = $oauth_user->getEmail();
            $password = Str::random(8);
            $user = User::create(compact('name', 'email', 'password'));
        }

        Auth::login($user);

        return redirect()->intended('dashboard');
    }