Auth user not saved in Laravel package

This is my first attempt at the laravel package and there was a problem when Auth :: attempt ($ credentials) works in my login controller, but when redirecting to a secure route or controller, the user is no longer authenticated. Below is my login controller method with redirect comments to the control panel.

public function attempt(Request $request){ $email = strtolower(strip_tags(htmlspecialchars($request->input('email')))); $password = strip_tags(htmlspecialchars($request->input('password'))); if (Auth::attempt(array('email' => $email, 'password' => $password))) { // Redirect to dashboard route //return redirect()->intended('/admin'); if(Auth::check()) print_r(Auth::user()); } } 

Responding to valid credentials displays the correct user account, and Auth :: check returns true. But when redirecting to the administrator controller, the user is not authenticated. Below is the method of the administrator controller, which should output an authenticated user, but returns only "not registered".

 public function index() { if(Auth::check()) print_r(Auth::user()); else echo "not logged"; } 

Both controllers use Auth; their namespaces are consistent with vendor / package / pathToDir, db is configured correctly, and the encryption key is set. Any ideas on what's going wrong? Thanks

+6
source share
2 answers

It turns out that the problem is with the new middleware on the Internet, moved all my routes that require session data to a group of routes, and everything works fine.

 Route::group(['middleware' => ['web']], function () { Route::get("/login", ['uses'=>' SiteLogin@index ']); Route::post("/login", ['uses'=>' SiteLogin@attempt ']); Route::get("/logout", ['uses'=>' SiteLogin@logout ']); Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () { Route::get('/', ['uses'=>' Admin@index ']); }); }); 
+14
source

The default behavior of the method is that the user does not register the user.

You must change it to:

 if (Auth::attempt(array('email' => $email, 'password' => $password), false, true)) 

This way you set remember as false and login as true.

More on this here: https://laravel.com/docs/5.2/authentication

0
source

All Articles