Bug: auth.required middleware is not triggered on dynamic routes within app()->group() · Issue #32 · leafsphp/auth · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using auth.required middleware inside app()->group(), dynamic routes like /clients/{id} skip the middleware execution. This causes the controller to be executed even if there’s no authenticated user.
It works fine on static routes like /clients, but on dynamic ones, the middleware is ignored.
Steps to reproduce the behavior:
In your routes/web.php, define a middleware group:
Ensure your auth.required middleware redirects if the user is not logged in.
Access /cms/clients while logged out → ✅ Middleware works (redirects).
Access /cms/clients/1 while logged out → ❌ Controller runs, middleware is skipped.
Expected behavior
All routes (static or dynamic) inside the app()->group() with auth.required should be protected. If a user is not logged in, the middleware should redirect and prevent controller execution.
Additional context
Leaf version: v3.x
Middleware is registered via auth()->middleware('auth.required', callback)
DevTools log shows controller is called before the middleware on dynamic routes.
The text was updated successfully, but these errors were encountered:
Root cause
In Router.php, the current logic inside handle() uses a direct equality check on $route['route']['pattern'] === $uri, which fails for dynamic route patterns.
Fix
This PR replaces that brittle check with a preg_match to verify if the route pattern matches the URI:
This fix was tested in a real project using dynamic routes inside a group with middleware. After this change, middleware was properly triggered before controller execution.
When using auth.required middleware inside app()->group(), dynamic routes like /clients/{id} skip the middleware execution. This causes the controller to be executed even if there’s no authenticated user.
It works fine on static routes like /clients, but on dynamic ones, the middleware is ignored.
Steps to reproduce the behavior:
In your routes/web.php, define a middleware group:
Ensure your auth.required middleware redirects if the user is not logged in.
Access /cms/clients while logged out → ✅ Middleware works (redirects).
Access /cms/clients/1 while logged out → ❌ Controller runs, middleware is skipped.
Expected behavior
All routes (static or dynamic) inside the app()->group() with auth.required should be protected. If a user is not logged in, the middleware should redirect and prevent controller execution.
Additional context
Leaf version: v3.x
Middleware is registered via auth()->middleware('auth.required', callback)
DevTools log shows controller is called before the middleware on dynamic routes.
The text was updated successfully, but these errors were encountered: