I have created a new Laravel 5.2 installation. And I have run the following command to install the default Laravel authentication;
php artisan make:auth
The registration form works but the login just redirects home without logging in. And displays no errors when when I enter the wrong credentials.
This is my routes file:
Route::get('/', 'BaseController@index');
Route::get('/tutors', 'TutorsController@Index');
Route::get('/tutors/myrequest', 'TutorsController@RequestTutor');
Route::get('/tutors/{id}', 'TutorsController@show')->where(['id' => '[0-9]+']);
Route::get('/tutors/{GUID}', 'TutorsController@TutorByGUID')->where(['id' => '[A-Za-z]+[0-9]+']);
/********************Authentication routes**********************************/
Route::group(['middleware' => ['web']], function () {
Route::auth();
});
This is code from the AuthController
:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
This is the BaseController
which contains the home method;
<?php namespace App\Http\Controllers;
use App\Services\InterfaceService;
use App\Repositories\TutorRepository;
use App\Http\Requests;
use Illuminate\Http\Request;
class BaseController extends Controller {
protected $TutorRepository;
protected $InterfaceService;
public function __construct(InterfaceService $InterfaceService, TutorRepository $TutorRepository)
{
//$this->middleware('guest');
$this->InterfaceService = $InterfaceService;
$this->TutorRepository = $TutorRepository;
}
public function index()
{
$tutors = $this->TutorRepository->all();
$page_info = \Config::get('constants.App.Pages.home');
$this->InterfaceService->SetPageInfo($page_info);
return view('home', ['TopTutors'=>$tutors]);
}
} ?>
This is code from the login view.
<form role="form" method="POST" action="{{ url('/login') }}" id="login_form">
{!! csrf_field() !!}
<div class="mj_login_form">
<div class="form-group">
<input type="text" placeholder="Email" id="email" name="email" class="form-control" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
@endif
</div>
<div class="form-group">
<input type="password" placeholder="Your Password" id="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block"><strong>{{ $errors->first('password') }}</strong></span>
@endif
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 mj_toppadder20">
<div class="form-group pull-left">
<div class="mj_checkbox">
<input type="checkbox" value="1" id="check2" name="remember">
<label for="check2"></label>
</div>
<span> remember me</span>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 mj_toppadder20">
<div class="form-group pull-right">
<span><a href="{{ url('/password/reset') }}">forget password ?</a></span>
</div>
</div>
</div>
</div>
<div class="mj_pricing_footer">
<a href="#" onclick="$('#login_form').submit()">login Now</a>
</div>
</form>
That happens because your '/'
route is not under the web
middleware group and you're not using the auth
middleware for the routes you want to be authenticated
This means that when accessing that route, session is not available, and the auth
middleware doesn't fire, so authentication doesn't work.
Try to put all the routes in which you want to use session under the web
middleware group, and for the routes in which you want authentication use the auth
middleware:
Route::group( ['middleware' => ['web'] ], function () {
/* these routes use 'auth' middleware, so only an authenticated user will access*/
Route::group( ['middleware' => 'auth' ], function () {
Route::get('/', 'BaseController@index');
});
Route::auth();
});
A Note on Laravel version:
Keep in mind that if you're using Laravel version >= 5.2.27
the middleware web
is used by default on every route defined in routes.php
as you can see in app/Providers/RouteServiceProvider.php
:
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web'
], function ($router) {
require app_path('Http/routes.php');
});
}
In that case you can remove you statement for using the web
middleware group, but you'll only need to use auth
middleware for the authenticated routes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.