Skip to content

Latest commit

 

History

History
91 lines (66 loc) · 2.68 KB

README.md

File metadata and controls

91 lines (66 loc) · 2.68 KB

Laravel passport client

Total Downloads Latest Stable Version License

Introduction

Fork from Laravel Socialite to build a Laravel passport client library to OAuth2.0 authentication with Laravel port.

Install

$ composer require hanbz/passport-client

add the config at .env

CLIENT_ID=oauth_client
CLIENT_SECRET=oauth_secert
REDIRECT=your_callback_url
OAUTH_DOMAIN=your_oauth_server

add this section at config/service.php

    'passport' => [
        'client_id' => env('CLIENT_ID'),
        'client_secret' => env('CLIENT_SECRET'),
        'redirect' => env('REDIRECT'),
        'domain' => env('OAUTH_DOMAIN')
    ]

Usage

Use the package by Facades name PassportClient. The driver name is passport. It includes two functions redirect and user.

The redirect function will redirect user to the oauth server.

web.php
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']);

The user function will return the user info.

AuthController.php
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');
    }

Contributing

Thank you for considering contributing to Laravel passport client!

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

License

Laravel passport client is open-sourced software licensed under the MIT license.