-
-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a method to force a redirect to a non-Inertia URL #57
Comments
Yep, I like it. I've had a use-case for this once when redirecting to a 3rd party auth provider. Not sure on two things:
|
I have come across the use-case where logout gets redirected to a normal blade rendered page in laravel. The option 2 will be very handy. |
I've also had to use this, both with logout links & Socialite redirects. Would be really handy to have in core |
Thanks @sebastiandedeyne I was going nuts with the logout.
|
I would love to see this feature as well. Building an inertia checkout code and need to have an ability to redirect the user to external payment gateway. |
A rails version of the above workaround, for rails users come here via google search # application_controller.rb
# Use `409 Conflict` to force client <InertiaApp> to go to +url+ with a full-page refresh
def redirect_without_inertia(url:)
headers['X-Inertia-Location'] = url
head :conflict
end |
Here is another interesting use case for this feature. Consider if you have a page in your app that isn't an Inertia page, but you somehow end up redirecting to that page via an Inertia request. You can use the asset conflict handling to force a full page load to these pages. Something like this: class LoginController extends Controller
{
public function showLoginForm()
{
if (Request::inertia()) {
return response('', 409)
->header('X-Inertia-Location', url()->current());
}
return view('auth.login');
}
} |
I think I might have another (similar) use case for this. My app has to authenticate at Shopify which involves a redirect offsite to do an oauth handshake. I therefore have to have my login form be a plain old POST form with an action and a hidden crsf token field. This all works fine but the problem occurs when a session times out. I'm redirected to my login screen as expected on session expiry but a subsequent attempt to login results in a 419 token mismatch. This makes sense as there needs to be a full page refresh to get a new session token. I've tried the solution in the snippet above and it works though it would be great to have a public function showLoginForm()
{
if (Request::inertia()) {
return Inertia::hardVisit('Pages/Login')
}
return Inertia::render('Pages/Login')
} Edit: on reflection I'm not really sure the above snippet makes much sense (or at least it might be a request for slightly different problem). I guess what I'm wondering is whether there could be a way of forcing an inertia view to hard reload under certain circumstances? |
I had an issue where I had to throw an exception in the class Authenticate extends Middleware
{
protected function redirectTo($request)
{
if (! $request->expectsJson() && Request::inertia()) {
abort(409, '', ['X-Inertia-Location' => url()->route('login')]);
} else if (! $request->expectsJson()) {
return route('login');
}
}
} |
How about
@Livijn You could just override the Edit: I'm wrong - the value from that method isn't returned as I first thought - but I think the |
thanks it works |
@opanegro You no longer need to do this manually. The Laravel adapter now has a method for this, called class LoginController extends Controller
{
public function showLoginForm()
{
if (Request::inertia()) {
return Inertia::location(url()->current());
}
return view('auth.login');
}
} More on that here. |
well done, thanks @reinink |
wow thanks @reinink |
I would suggest a mention under Thank you |
@danrichards Hey there! So, this feature is already on the redirects page: https://inertiajs.com/redirects#external-redirects |
Amazing work, man! Amazing work! @reinink |
this is my solution for that in the front i check the response is not a valid inertia response and is a 200 status then I take the URL from the response and I make a redirection with window.location
|
This is brilliant! One possible addition to the Laravel example in the Inertia documentation is that this can be implemented as middleware and then quite easily applied to multiple routes. It makes it easy to then set apart routes that you know will never return an inertia response from those that will. It also might help with apps where devs want to implement Inertia into Laravel progressively. Middleware like this: class NonInertiaRoutes
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if ($request->inertia()) {
return Inertia::location($request->fullUrl());
}
return $next($request);
}
} Allows you to wrap routes like this: // Non Inertia route for redirects to GrapesJS pages
Route::middleware(['non-inertia'])->group(function () {
Route::get('/', [PageController::class, 'show']);
}); I had an issue using Laravel Breeze set up for Vue.js alongside laravel-grapesjs where if you visited a page to be edited by grapesjs before logging in (for example after session expiry) then you were redirected to login page and then again redirected to the edit route. But the edit route was not going to return an Inertia response so I would get the modal. Alternatively if I logged out and wanted to be redirected to a grapesjs created page I would have the same issue. In both cases I did not have control of the link that sent users to the URL as they were redirects - so this came in handy when trying to 'pop-out' of inertia for those specific routes. |
thanks @bluekable |
I was able to implement Inertia::location() with Fortify like this
|
Sometimes we want to redirect the user to a non-Inertia environment after a visit.
Inertia already supports something similar under the hood, for asset versioning.
https://github.com/inertiajs/inertia/blob/da004ee147cf53a7f02804f50dc0a324ecb9fdec/src/inertia.js#L83-L85
We added this little helper function in our app, but it feels hacky:
Would it make sense to add an
Inertia::forceRedirect($url)
method to inertia-laravel?The text was updated successfully, but these errors were encountered: