Skip to content

Instantly share code, notes, and snippets.

@kresnasatya
Last active March 14, 2024 12:07
Show Gist options
  • Save kresnasatya/5031ee18b25e317341c52bc6a7c39671 to your computer and use it in GitHub Desktop.
Save kresnasatya/5031ee18b25e317341c52bc6a7c39671 to your computer and use it in GitHub Desktop.
Fixed problem Laravel Passport doesn't redirect back to authorization form after login

I have been facing the same problem and search in Laravel Passport's issue: Passport doesn't redirect back to authorization form after login. I saw that issue #248 and #703 get this problem too. But, I don't find the answer to solve this problem.

Suddenly, I remember that I have been implement GitHub OAuth2 with Laravel Socialite and I think the flow is same because it using Authorization code flow grant type.

First, I inspect network, check url address bar for GitHub OAuth2 flow with Laravel Socialite.

Second, I analyze what's going on and find out that route oauth/authorize bind by auth middleware after run php artisan route:list. So, I just modify the Authenticate middleware, login controller, and login view. I run php artisan make:auth to generate auth scaffolding.

Third, I just play around with dd() to get result of $request (Laravel Request API).

Modified files attached below.

Hope it helps!

<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
// Start modified line
if ($request->path() === 'oauth/authorize') {
if (isset($request->query()['client_id'])) {
$params = array(
'client_id' => $request->query()['client_id'],
'return_to' => \Request::getRequestUri(),
);
return route('login', $params);
} else {
return route('login');
}
}
// End modified line
return route('login');
}
}
}
<!-- Just replace value of action with $request_uri -->
<!-- Before -->
<form method="POST" action="{{ route('login') }}">
<!-- After -->
<form method="POST" action="{{ $request_uri }}">
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginForm(Request $request)
{
// Add request_uri as data.
// So, when user submit username and password for login then value of request uri will redirect user to login method
return view('auth.login', array('request_uri' => \Request::getRequestUri()));
}
public function login(Request $request)
{
// Let's assume that user is authenticated and decide which route will be redirect user
if (isset($request->query()['return_to'])) {
// Then redirect to /oauth/authorize?blablabla
return redirect($request->query()['return_to']);
} else {
return redirect('/home');
}
}
}
@Akellacom
Copy link

Thank you!

@infureal
Copy link

For new versions authorize route isn't binded with auth middleware :(

@kresnasatya
Copy link
Author

For new versions authorize route isn't binded with auth middleware :(

Sorry, I don't try in the new version. If you have try in the new Laravel version, please don't mind to share in here. Thank you.

@lukas-staab
Copy link

This worked for me with Laravel Breeze (switching from Sanctum to Passport)

laravel/passport#248 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment