Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel auth not displaying session message

I am trying to authenticate a user type in laravel 5.2. The route is as below :

Route::group(['middleware' => ['web']], function(){
    Route::auth();
    Route::get('/user/department/login', ['as' => 'department_login', 'uses' => 'DepartmentUserAuth\AuthController@showLoginForm']);
    Route::post('/user/department/login','DepartmentUserAuth\AuthController@deptLoginPost');

}); 

And the AuthController.php is

public function deptLoginPost(Request $request)
{

    $this->validate($request, [
        'username'      => 'required',
        'password'      => 'required',
    ]);
    if (auth()->guard('department_user')->attempt(['username' => $request->input('username'), 'password' => $request->input('password')]))
    {
        $user = auth()->guard('department_user')->user();
        dd($user);
    }else{
       return redirect('/user/department/login')->with('message', 'Invalid credentials');
    }
}

And the login.blade.php is :

@if(Session::has('message'))
<div class="row">
   <div class="col-lg-12">
         <div class="alert {{ Session::get('alert-class', 'alert-info') }}">
               <button type="button" class="close" data-dismiss="alert">×</button>
               {!! Session::get('message') !!}
         </div>
      </div>
</div>
@endif

{{ dump(session()->all()) }}

But the Session::has('message') is empty, and the dump shows as below :

array:3 [▼
  "_token" => "BvQ630KrUl78Ngb8d3cst6pAIqenra3ohbYbLzAP"
  "_previous" => array:1 [▼
    "url" => "http://localhost:8080/material/public/user/department/login"
  ]
  "flash" => array:2 [▼
    "old" => []
    "new" => []
  ]
]

How can I display all error messages ( as in default auth controller ) in my login.blade.php ?

like image 489
Nitish Avatar asked Mar 07 '23 20:03

Nitish


2 Answers

Try setting the message directly on the request's message:

$request->session()->flash('message', 'Invalid credentials');

example:

if (auth()->guard('department_user')->attempt(['username' => $request->input('username'), 'password' => $request->input('password')]))
{
    $user = auth()->guard('department_user')->user();
    dd($user);
}else{
    $request->session()->flash('message', 'Invalid credentials');
    return redirect()->back();
}

If still doesn't help, look into your routes and see if your middleware is assigned properly.

// either as a string
Route::group(['middleware' => 'web']...
// or as a array element
Route::group(['middlewareGroups' => ['web']]...
like image 129
Unamata Sanatarai Avatar answered Mar 10 '23 11:03

Unamata Sanatarai


This should be working, as far as I can tell (in fact, I just tried this in a fresh Laravel 5.2 installation and it worked as expected). My only guess is, the with method doesn't write the message "permanently" in the session, but in the flash data (only available for the next request cycle). If, for some reason, you are getting a redirection response, that response will consume the flash data, and by the time you reach the login form again, the message is no longer in the session.

So, are you doing any redirection other than the one in the AuthController? Maybe in a middleware?

Some things you could try:

  • Add some middleware that logs every request (see, how to log every response in Laravel 5.2 framework) so you can trace if there are additional redirects.

  • Add session()->reflash(); to a middleware that runs with every request. (If this work this should prove my point that the flash data is being consumed in an intermediate request).

  • Replace

    return redirect('/user/department/login')
        ->with('message', 'Invalid credentials');
    

    with

    return redirect()
        ->back()->with('message', 'Invalid credentials');
    
  • Replace (only temporarily, to test you can write and read from the session store)

    return redirect('/user/department/login')
        ->with('message', 'Invalid credentials');
    

    with

     session(['message' => 'Invalid credentials']);
     return redirect('/user/department/login');
    
like image 42
alepeino Avatar answered Mar 10 '23 09:03

alepeino