hanbz/passport-client

Laravel wrapper around passport client OAuth2 libraries.
12,203 1
Install
composer require hanbz/passport-client
Latest Version:v1.0.1
PHP:^7.2|^8.0
License:MIT
Last Updated:Nov 13, 2020
Links: GitHub  ·  Packagist
Maintainer: hanbzsky

Laravel passport client

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.

Related Packages

14four/oauth2-basecamp

Basecamp OAuth 2.0 Client Provider for The PHP League OAuth2-Client

1,628 3
sas1024/socialite-xenforo-bdapi

XenForo over [bd] Api OAuth2 Provider for Laravel Socialite

32 3
sdclub/laravel-rest-api-client

An elegant and smart Rest API Client with OAuth2 authentication support. Build f...

34 0
someline/rest-api-client

An elegant and smart Rest API Client with OAuth2 authentication support. Build f...

13,484 8
n30/socialite-amazon

Laravel Socialite for Login with Amazon/AWS Accounts. Social Providers Extention...

3,300 1