2014-01-09 22 views
0

我有一個奇怪的問題,我的Laravel-4 Authentication。只有我的第一個數據庫行可以登錄我的Laravel項目,其他人返回false。Auth :: attempt()只在數據庫行1上返回true

我使用身份驗證有點不同於預期。我沒有用戶名,只有一個密碼,也稱爲「代碼」。我也試過用戶名(如「party」),但這似乎也不起作用。

我在我的數據庫中添加了一個表「password_unh」(= password unhashhed),當然這不是它最終應該怎麼做,而是檢查我的密碼(因爲我的密碼錶被哈希),以及我也看到這是我的'派對'。

這裏是我的代碼(我把代碼關於「黨在評論)

login.blade.php

@extends("layout") 
@section("content") 
    {{ Form::open([ 
     "route"  => "party/login", 
     "autocomplete" => "off" 
    ]) }} 
     <!--{{ Form::label("party", "Party") }} 
     {{ Form::text("party", Input::get("party"), [ 
      "placeholder" => "Christmas party Smith" 
     ]) }}--> 
     {{ Form::label("code", "Code") }} 
     {{ Form::password("code") }} 

     @if($errors->has()) 
      @foreach ($errors->all() as $error) 
       <div class='error'>{{ $error }}</div> 
      @endforeach 
     @endif 
     {{ Form::submit("login") }} 
    {{ Form::close() }} 
@stop 
@section("footer") 
    @parent 
    <script src="//polyfill.io"></script> 
@stop 

UserController.php

<?php 
use Illuminate\Support\MessageBag; 
class UserController extends Controller 
{ 
    public function loginAction() { 

     $errors = new MessageBag(); 
     if ($old = Input::old("errors")) 
     { 
      $errors = $old; 
     } 
     $data = [ 
      "errors" => $errors 
     ]; 
     if (Input::server("REQUEST_METHOD") == "POST") 
     { 
      $validator = Validator::make(Input::all(), [ 
       //"party" => "required", 
       "code" => "required" 
      ]); 

      if ($validator->passes()) 
      { 
       $credentials = [ 
        //"password_unh" => Input::get("party"), 
        "password" => Input::get("code") 
       ]; 
       if (Auth::attempt($credentials)) 
       { 
        return Redirect::to('profile'); 
       } 
      } 
      $data["errors"] = new MessageBag([ 
       "code" => [ 
        "Party and/or code invalid." 
       ] 
      ]); 
      //$data["party"] = Input::get("party"); 

      return Redirect::route("party/login") 
       ->withInput($data); 
     } 
     return View::make("party/login", $data); 
    } 
} 

數據庫用戶表 enter image description here

回答

0

當然,你應該有leas t usernamepassword字段$credentials數組。 我可以看到你在數據庫表中也錯過了。

+0

的確!愚蠢但正確,謝謝! – Valerie

+1

要注意,你可以在'$ credetials'中添加其他字段(它們必須與'users'表中的列名匹配)。例如,如果您設置了'$ credentials ['active'] = true;',Laravel僅查找在數據庫表中設置爲'active = 1'的用戶... – Andreyco

0

這是不理想,但你可以做

$user = User::where('password',Hash::make(Input::get('code'))); 

if(Auth::loginUsingId($user->id)) 
{ 
    //do blah 
} 

if(Auth::login($user)) 
{ 
    //do blah 
}