Last active
August 25, 2021 11:20
-
-
Save d8vjork/f5065bf6ca34488c6e53d16a5972d340 to your computer and use it in GitHub Desktop.
Laravel Passport with Inertia as authorize's response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->register(PassportServiceProvider::class); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Support\Arr; | |
use Inertia\Inertia; | |
class PassportServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->when(\Laravel\Passport\Http\Controllers\AuthorizationController::class) | |
->needs(\Illuminate\Contracts\Routing\ResponseFactory::class) | |
->give(fn () => app(CustomResponseFactory::class)); | |
} | |
} | |
class CustomResponseFactory extends \Illuminate\Routing\ResponseFactory | |
{ | |
/** | |
* Create a new response for a given view. | |
* | |
* @param string|array $view | |
* @param array $data | |
* @param int $status | |
* @param array $headers | |
* @return \Illuminate\Http\Response | |
*/ | |
public function view($view, $data = [], $status = 200, array $headers = []) | |
{ | |
return Inertia::render( | |
'Auth/Authorize', | |
array_merge( | |
Arr::except($data, 'request'), | |
['state' => $data['request']->state] | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment