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.
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 viewsresources\views\auth
and copy all resources files to it from https://github.com/laravel/laravel/tree/5.0/resources/views/auth3- Create
css
andfonts
folder insidepublic
folder and copy both folders content from resources https://github.com/laravel/laravel/tree/5.0/public4- Update your route file like below
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 editAuthController.php
inapp\Http\Controllers\Auth
:After this line
Add the code with your location
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
Browser snapshot of Login page
It is also possible to make a simpler authentication template by following Laravel documentation.
Laravel authentication documentation http://laravel.com/docs/5.1/authentication