Laravel 5 user can not be retrieved after successf

2020-02-15 07:55发布

I am using Laravel 5.1 and trying to implement user authentication. Basically no routes should be accessible without loggin in. The built in trait (AuthenticatesAndRegistersUsers) does not work correctly since, after a successful authentication, the redirected controller will not be able to pull out the user via Auth::user() or $this->auth->user or $request->user().

I did verify that it is a successful attempt by var-dumping the user right after $this->auth->attempt().

However after the redirect with return redirect()->intended(...), no user can be pulled.

I am now trying to implement it by skipping the AuthenticatesAndRegistersUsers and writing it directly in the AuthController but again encountering the same problem.

This is the part in AuthController that does the validation:

$this->validate($request, [
    'email'    => 'required|email',
    'password' => 'required',
]);

$credentials = $request->only('email', 'password');

//attempt to authenticate
//if successful, redirect to the intended url or the default redirectPath
if ($this->auth->attempt($credentials, $request->has('remember')))
{
    //when I do dd($this->auth->user()) here it shows the correct user record
    return redirect()->intended($this->redirectPath);
}

It redirects to the correct url (HomeController) but then the user can not be retrieved (within the show method or any other method):

dd(Auth::user()); //null
dd(Auth::check());//null

HomeController extends my BaseController which uses the auth middleware

public function __construct()
{
    $this->middleware('auth');
}

I had Laravel 4 custom authentication and it was all working fine, I'm still new to the middleware authentication concept in Laravel 5.

Any help would be appreciated.

1条回答
做个烂人
2楼-- · 2020-02-15 08:27

How to use the build-in authentication in Laravel 5.1

Laravel 5.1 has build in authentication and it works almost out of the box.

There are few step to do, so you will be able to use the nice default build in Authentication template in Laravel 5.1. So you do not need to invent that wheel again like Laravel 4.0.

1- Install fresh copy of Laravel (follow this http://laravel.com/docs/5.1) I did it by simply using this command composer create-project laravel/laravel --prefer-dist

2- Create auth folder in views resources\views\auth and copy all resources files to it from https://github.com/laravel/laravel/tree/5.0/resources/views/auth

3- Create css and fonts folder inside public folder and copy both folders content from resources https://github.com/laravel/laravel/tree/5.0/public

4- Update your route file like below

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

regarding to https://github.com/laravel/laravel/blob/5.0/app/Http/routes.php

5- Create empty database in your MySQL.

6- Update your .env file with database info etc.

7- Run php artisan migrate

8- Go to browser and write http://YourLaravelHostProject/auth/register

9- (Optional) If you want to change /home landing page after login/registration process edit AuthController.php in app\Http\Controllers\Auth:

After this line

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

Add the code with your location

//forward after login
protected $redirectPath = '/YourLocation'; 

That is it. Now you have the default login/register/reset password template working. You can modify it for further use.

php artisan migrate results enter image description here

Browser snapshot of Login page enter image description here

It is also possible to make a simpler authentication template by following Laravel documentation.

Laravel authentication documentation http://laravel.com/docs/5.1/authentication

查看更多
登录 后发表回答