2015-05-29 393 views
0

我遇到了問題,我的身份驗證不斷收到else語句,同時我在登錄表單中輸入了正確的電子郵件和密碼。隨着else語句我的意思是我SessionController的最後一行Laravel身份驗證失敗

else 

    { 
     return Redirect::to('login') 
    } 

這裏是我的routes.php文件

// ~ Root 
Route::get('/', array('as' => 'root', 'uses' => '[email protected]')); 

// ~ Session ~ Login ~ Logout 
Route::get('login', array('as' => 'newSession', 'uses' => '[email protected]')); 
Route::post('login', array('as' => 'setSession', 'uses' => '[email protected]')); 
Route::get('logout', array('as' => 'destroySession', 'uses' => '[email protected]')); 

這裏是我的SessionController

<?php 

class SessionController extends BaseController { 

    public function newSession() { 

     $this->layout->content = View::make('login'); 

    } 

    public function setSession() { 

     $rules = array(
      'email' => 'required|email', 
      'password' => 'required|alphaNum|min:3' 
     ); 

     $validator = Validator::make(Input::all(), $rules); 

     if ($validator->fails()) { 
      return Redirect::to('login') 
       ->withErrors($validator) 
       ->withInput(Input::except('password')); 
     } else { 

      // create our user data for the authentication 
      $userdata = array(
       'email'  => Input::get('email'), 
       'password' => Input::get('password') 
      ); 

      // attempt to do the login 
      if (Auth::attempt($userdata)) { 

       return Redirect::to('asd'); 

      } else { 
       return Redirect::to('login') 
       ->withErrors($validator) 
       ->withInput(Input::except('password')); 
       // validation not successful, send back to form 
       // return Redirect::to('login')->withErrors($validator); 
      } 
     } 
    } 

    public function destroySession() { 

     Auth::logout(); 
     return Redirect::to('login')->with('message', 'You are logged out'); 

    } 
} 

這是我的形式

{{ Form::open(array('action' => '[email protected]', 'method' => 'POST', 'class' => 'form-horizontal navbar-form navbar-right')) }} 
<div id="navbar" class="navbar-collapse collapse"> 
    <div class="form-group"> 
     <p> 
      {{ $errors->first('email') }} 
      {{ $errors->first('password') }} 
     </p> 
     <p> 
      {{ Form::label('email', 'Email Address', array('class' => 'navLogTxt')) }}   
      {{ Form::text('email', Input::old('email'), array('class' => 'form-control')) }}  
      {{ Form::label('password', 'Passwort', array('class' => 'navLogTxt')) }}    
      {{ Form::password('password', array('class' => 'form-control')) }} 
      {{ Form::submit('Anmelden!', array('class' => 'btn btn-success')) }} 
     </p>       
    </div> 
</div> 
{{ Form::close() }} 

,最後,這是我的播種機

<?php 

class UserTableSeeder extends Seeder { 

    public function run() { 

     DB::table('users')->delete(); 
     User::create(array(
      'email' => '[email protected]', 
      'password' => Hash::make('awesome') 
     )); 
     User::create(array(
      'email' => '[email protected]', 
      'password' => 'test' 
     )); 

    } 

} 
+0

密碼是在數據庫中加密還是以純文本形式存儲? – Bogdan

+0

看看[這個答案](http://stackoverflow.com/a/15673247/351330),看看是否可以解決你的問題。 – Bogdan

+0

密碼以明文形式存儲 – utdev

回答

0

Laravel Authentication Documentation摘自:

記住:建立此模型的數據庫模式時,使密碼列至少60個字符。另外,在開始之前,請確保您的用戶(或同等)表包含一個可爲空的字符串,包含100個字符的字符串remember_token列。

爲了將來的參考,Laravel附帶了users表的移植,您可以在database/migrations/2014_10_12_000000_create_users_table.php中找到該表。您可以使用它通過運行php artisan migrate來生成users表。

您當然可以在此定義的默認值之外添加其他列,只需閱讀Schema Builder Documentation以獲取更多信息。

+0

我試過這個,但仍然不起作用 – utdev

+0

將列調整爲60個字符後,您是否重新創建用戶?因爲如果你不重新散列它,密碼值仍然是50個字符。 – Bogdan

+0

謝謝你完美的工作! – utdev