Laravel auth does not display session message

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

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 AuthController.phpthere 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 login.blade.php:

@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 Session::has('message')empty, and dumpshown 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 auth controller by default) in mine login.blade.php?

+4
source share
4 answers

Try setting up the message directly in the request 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 all else fails, take a look at your routes and see if your middleware is properly assigned.

// either as a string
Route::group(['middleware' => 'web']...
// or as a array element
Route::group(['middlewareGroups' => ['web']]...
+3
source

, ( , Laravel 5.2 , ). : with "" , - ( ). - , -, .

, , , AuthController? , ?

, :

  • , (., Laravel 5.2), , .

  • session()->reflash(); , . ( , , ).

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

    return redirect()
        ->back()->with('message', 'Invalid credentials');
    
  • ( , , )

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

     session(['message' => 'Invalid credentials']);
     return redirect('/user/department/login');
    
+2

, routes.php web , .

Route::auth() Laravel, , , .

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

+1

In addition, using the facade Session:

//display error message
Session::flash('message', 'Invalid credentials')

//color the output
Session::flash('alert-class', 'alert-danger')

Or, laracasts / flash , you could:

//AuthController
flash('Invalid credentials')->error();

//login.blade.php
@include('flash::message')
+1
source

Source: https://habr.com/ru/post/1687410/


All Articles