0
votes

Should a redirect with flash data persist the flash data if the auth middleware is involved?

A few housekeeping things to note that will answer some possible followup questions:

    1. I am calling the web middleware.
    1. I'm using the file sessions driver.
    1. I can retrieve values stored in the session with the exception of flashed data.
    1. I have tried reflashing the flashed data by adding the following line to the Authenticate middleware:
      $request->session()->reflash();
      

As such, Authenticate.php now appears as follows:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->guest()) {
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login');
        }
    }

    $request->session()->reflash();

    return $next($request);
}

This issue is also affecting the auth boilerplate generated by make:auth, resulting in the $errors failing to display on error.

UPDATE (3/29 @ 08:54 EST)

I uncovered what I believe to have been the root cause, as you will see below. Each route was calling 'web' middleware twice. As such, two requests were actually taking place which was removing the flash message(s) before the user had a chance to see them. Original route:list is below.

+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
| Domain | Method   | URI                     | Name | Action                                                          | Middleware    |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
|        | GET|HEAD | /                       |      | Closure                                                         | web           |
|        | GET|HEAD | groups                  |      | App\Http\Controllers\GroupsController@index                     | web,web,auth  |
|        | GET|HEAD | groups/set-default/{id} |      | App\Http\Controllers\GroupsController@setDefaultGroup           | web,web,auth  |
|        | GET|HEAD | home                    |      | App\Http\Controllers\HomeController@index                       | web,web,auth  |
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm          | web,web,guest |
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login                  | web,web,guest |
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout                 | web,web       |
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,web,guest |
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset              | web,web,guest |
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm      | web,web,guest |
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | web,web,guest |
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register               | web,web,guest |
|        | GET|HEAD | visitees                |      | App\Http\Controllers\VisiteesController@index                   | web,web,auth  |
|        | GET|HEAD | visitees/check-in/{id}  |      | App\Http\Controllers\VisiteesController@checkIn                 | web,web,auth  |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+

My routes now appear as follows after removing the routes from the 'web' middleware:

+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
| Domain | Method   | URI                     | Name | Action                                                          | Middleware |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
|        | GET|HEAD | /                       |      | Closure                                                         | web        |
|        | GET|HEAD | groups                  |      | App\Http\Controllers\GroupsController@index                     | web,auth   |
|        | GET|HEAD | groups/set-default/{id} |      | App\Http\Controllers\GroupsController@setDefaultGroup           | web,auth   |
|        | GET|HEAD | home                    |      | App\Http\Controllers\HomeController@index                       | web,auth   |
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm          | web,guest  |
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login                  | web,guest  |
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout                 | web        |
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest  |
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset              | web,guest  |
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm      | web,guest  |
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | web,guest  |
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register               | web,guest  |
|        | GET|HEAD | visitees                |      | App\Http\Controllers\VisiteesController@index                   | web,auth   |
|        | GET|HEAD | visitees/check-in/{id}  |      | App\Http\Controllers\VisiteesController@checkIn                 | web,auth   |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+

Upon moving the routes out of the 'web' middleware group, the flash message display correctly. But, now I have a new issue!

The flash messages are not being removed from the session after the initial request. They persist each subsequent request until they are manually flushed or forgotten.

I'm not sure at this point if I should open up a second question that specifically addresses the persisting of the flash data. Please advise if so.

1
mind to show the output of issue artisan command php artisan route:list ?terry low
@terrylow I added the route:list per your request. I made some additional discoveries and will continue to dive into this issue. All help is appreciated!Wes Dollar
pls restore back your routes and perform a composer update, the double web middleware auth routes bug has already been resolved with the release of laravel/framwork 5.2.27,then run php artisan make:auth btw remove the line you have manually reflash session, and your problem should be no moreterry low
@terrylow I updated to 5.2.27. My routes still show the web middleware as being applied twice on my routes. The routes associated with Route::auth() are correct. I had to move the rest of my routes back out of the 'web' middleware in order to remove the duplicate 'web.' The flash messages again appear, but they persist upon subsequent requests, including those associated with auth $errors.Wes Dollar
thats weird, so if your routes are not in web middleware, is the flash messages works fine ? did you remove the line $request->session()->reflash(); ?terry low

1 Answers

0
votes

please run a composer update to update laravel/framework to v5.2.27, then issue php artisan make:auth to regenerate auth routes