laravelista/loki

Handling 403 exception redirect.

jeff-h opened this issue ยท 7 comments

Instead of showing a 403 page I've got an unauthenticated() exception handler which does this:

return redirect()->guest('login');

Unfortunately it's not picking up on the correct locale. For example, /de/profile is redirecting to /en/login.

I noticed Loki's middleware was not actually running in that request, so I tried reordering it in Loki.php's mapLocalizedWebRoutes():

Route::middleware(['loki', 'web'])

instead of

Route::middleware(['web', 'loki'])

but unfortunately that didn't help.

Any thoughts?

This could happen if you've placed auth routes to be translated with loki in the routes file.

Loki can only "translate" routes if they are named routes. Auth::routes() does not name routes as far as I know. You could manually create login, logout, register, etc.. routes and name them. Then, when redirecting always use route names. That should work as a workaround for now.

I'm planning to rewrite loki in a month or two. See this comment for more info. The new version will work without named routes and it will more stable.

Thanks for your response โ€” greatly appreciated! I will definitely try naming my admin routes first thing tomorrow, and will add a comment here with the result.

Hmm, turns out Auth::routes() does name routes: see here (with the inexplicable exception of a few post routes). I'm still on 6.x and its auth routes code is pretty much the same.

Regardless, I tried shifting those routes into my loki.web.php and naming every single one, and using

return redirect()->route('login');
// instead of:
// return redirect()->guest('login');

...but still no joy.

I do still suspect my issue is because the Loki middleware is simply not being run. As mentioned I tried reordering it so it runs before the web middleware.

In this case, in the Loki middleware the code $route->getPrefix(); returns de/profile when I visit the url /de/profile, for example. I am not sure where Laravel would usually set the correct prefix (ie to just de) โ€” still digging on that front.

Quick question: have you used Loki with routes using auth middleware successfully?

I must admit that I haven't used it with auth middleware or with Auth::routes(). Routes located in loki.web.php should use the loki middleware by design.

If you check php artisan route:list you should see (depending on your loki configuration) route names en.login, de.login. If you have enabled that the default language should be hidden, then you will also see route name login.

The package is very simple to understand, but the biggest mistake that I have done is that I have extended the UrlGenerator class. In most cases that causes issues since Laravel uses some of the methods that I have extended in different scenarios.

Also, there is an issue with Laravel 6 for which I had to implement a patch here

I'm sorry that this is causing you this much trouble. I will be rewriting loki in the future because there are a lot of cases that I have stumbled upon where loki is lacking. If possible try using mcamara/laravel-localization package for now. I have seen that they have fixed a lot of issues with it.

Thanks for your help again. I agree, the pragmatic thing in my case is to move to mcamara/laravel-localization as you said, for now anyway. Bit sad as I really like how readable and minimal your solution is :) I'll definitely keep an eye out on this package!

It is sad for me too ๐Ÿ˜Š

I use it in production for several applications... back to the design board it seems