Coder Social home page Coder Social logo

Comments (21)

reinink avatar reinink commented on August 19, 2024 16

@opanegro You no longer need to do this manually. The Laravel adapter now has a method for this, called Inertia::location. For example:

class LoginController extends Controller
{
    public function showLoginForm()
    {
    	if (Request::inertia()) {
            return Inertia::location(url()->current());
    	}

        return view('auth.login');
    }
}

More on that here.

from inertia-laravel.

reinink avatar reinink commented on August 19, 2024 15

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

from inertia-laravel.

bluekable avatar bluekable commented on August 19, 2024 5

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.

from inertia-laravel.

fourstacks avatar fourstacks commented on August 19, 2024 3

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 hardVisit solution that doesn't trigger a browser console error and if possible otherwise allows us to use the same Inertia API e.g.

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?

from inertia-laravel.

reinink avatar reinink commented on August 19, 2024 3

@danrichards Hey there! So, this feature is already on the redirects page: https://inertiajs.com/redirects#external-redirects

from inertia-laravel.

jartaud avatar jartaud commented on August 19, 2024 2

Thanks @sebastiandedeyne I was going nuts with the logout.

use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

if (!function_exists('redirectWithoutInertia')) {
    function redirectWithoutInertia(string $url)
    {
        return response('', SymfonyResponse::HTTP_CONFLICT)->header('x-inertia-location', $url);
    }
}

from inertia-laravel.

crynobone avatar crynobone commented on August 19, 2024 1

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.

from inertia-laravel.

choznerol avatar choznerol commented on August 19, 2024 1

We added this little helper function in our app, but it feels hacky: ...

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

from inertia-laravel.

danrichards avatar danrichards commented on August 19, 2024 1
Inertia::location(url()->current());

I would suggest a mention under Manual Visits or Redirects in the docs. I wish I found this faster.

Thank you

from inertia-laravel.

reinink avatar reinink commented on August 19, 2024

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:

  1. If using the conflict HTTP code is right for this use case. I almost wonder if we should have some type of Inertia "hard visit" response that's proper JSON.
  2. If forceRedirect is the right language. Right now it's called a "hard visit". Maybe it should be Inertia::hardVisit($url)? Admittedly that isn't as obvious as the word redirect.

from inertia-laravel.

raftalks avatar raftalks commented on August 19, 2024

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.

from inertia-laravel.

m1guelpf avatar m1guelpf commented on August 19, 2024

I've also had to use this, both with logout links & Socialite redirects. Would be really handy to have in core

from inertia-laravel.

Livijn avatar Livijn commented on August 19, 2024

I had an issue where I had to throw an exception in the Authenticate middleware since it doesn't allow for setting headers.

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

from inertia-laravel.

 avatar commented on August 19, 2024

Not sure [...] If forceRedirect is the right language. Right now it's called a "hard visit". Maybe it should be Inertia::hardVisit($url)? Admittedly that isn't as obvious as the word redirect.

How about hardRedirect()?

I had an issue where I had to throw an exception in the Authenticate middleware since it doesn't allow for setting headers.

@Livijn You could just override the unauthenticated() method from the parent class.

Edit: I'm wrong - the value from that method isn't returned as I first thought - but I think the Illuminate\Foundation\Exceptions\Handler::unauthenticated() method could be overridden in App\Exception\Handler.

from inertia-laravel.

crynobone avatar crynobone commented on August 19, 2024

image

I recently update the deps from v0.2.6 to v0.2.7 and using SymfonyResponse::HTTP_CONFLICT no longer working as expected.

from inertia-laravel.

ahmadrio avatar ahmadrio commented on August 19, 2024

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

thanks it works

from inertia-laravel.

aacassandra avatar aacassandra commented on August 19, 2024

well done, thanks @reinink

from inertia-laravel.

ahmadrio avatar ahmadrio commented on August 19, 2024

wow thanks @reinink

from inertia-laravel.

chrisidakwo avatar chrisidakwo commented on August 19, 2024

Amazing work, man! Amazing work! @reinink

from inertia-laravel.

hugo-abdou avatar hugo-abdou commented on August 19, 2024

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

Inertia.on("invalid", (event) => {
    if (event.detail.response.status == 200) {
        event.preventDefault();
        window.location.href = event.detail.response.request.responseURL;
    }
});

from inertia-laravel.

ahmadrio avatar ahmadrio commented on August 19, 2024

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

from inertia-laravel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.