2016-04-16 138 views
3

我創建了一個新的Laravel 5.2安裝。我已經運行以下命令來安裝默認的Laravel身份驗證;Laravel 5.2登錄不起作用

php artisan make:auth 

報名表的作品,但登錄只是重定向家裏沒有登錄。並顯示時,當我輸入了錯誤的憑據沒有錯誤。

這是我的路線文件:

Route::get('/', '[email protected]'); 

Route::get('/tutors', '[email protected]'); 
Route::get('/tutors/myrequest', '[email protected]'); 
Route::get('/tutors/{id}', '[email protected]')->where(['id' => '[0-9]+']); 
Route::get('/tutors/{GUID}', '[email protected]')->where(['id' => '[A-Za-z]+[0-9]+']); 


/********************Authentication routes**********************************/ 
Route::group(['middleware' => ['web']], function() { 
    Route::auth(); 
}); 

這是從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']), 
    ]); 
} 
} 

這是一個包含了家庭法BaseController;

<?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]); 
    } 

} ?> 

這是來自登錄視圖的代碼。

<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> 
+0

還請包括登錄視圖文件。 – Bogdan

+0

我想我應該添加Auth惡意軟件,但我不確定如何配置它與路線。我的嘗試導致登錄頁面出現無限循環。我也看過將\ App \ Http \ Middleware \ Authenticate :: class添加到Kernel.php $ middlewareGroups中,但那也不起作用。 – LogicDev

+2

從v5.2.27開始,'app/Http/routes.php'中的所有路由都已包含在'web'中間件組中。嘗試移除圍繞您的Route :: auth()的路由組。 – patricus

回答

1

那是因爲你的'/'路由不是web中間件組下和你不使用auth中間件要進行身份驗證

這意味着訪問該路線時的路線,會話不可用,並且auth中間件不會觸發,因此身份驗證不起作用。

儘量把所有你想要的web中間件組下使用會話路由,並要在其中路由認證使用auth中間件:

Route::group(['middleware' => ['web'] ], function() {  

    /* these routes use 'auth' middleware, so only an authenticated user will access*/ 
    Route::group(['middleware' => 'auth' ], function() { 
     Route::get('/', '[email protected]'); 
    }); 

    Route::auth(); 
}); 

上Laravel版本的注:

請記住,如果你使用Laravel版本> = 5.2.27中間件web默認情況下使用的routes.php定義的每個路線上,你可以看到app/Providers/RouteServiceProvider.php

protected function mapWebRoutes(Router $router) 
{ 
    $router->group([ 
     'namespace' => $this->namespace, 'middleware' => 'web' 
    ], function ($router) { 
     require app_path('Http/routes.php'); 
    }); 
} 

在這種情況下,你可以刪除你的語句使用web中間件組,但你只需要使用auth中間件認證的路線