2014-11-02 115 views
1

嗨,大家好我有一個laravel簡單的應用程序,我嘗試將用戶身份驗證添加到我的應用程序,這是我的route.php文件:驗證用戶Laravel

Route::model('task', 'Task'); 


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

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


Route::group(array('before'=>'auth'), function(){ 

Route::get('/', '[email protected]'); 
Route::get('/create', '[email protected]'); 
Route::get('/edit/{task}', '[email protected]'); 
Route::post('/edit', '[email protected]'); 
Route::post('/create' , '[email protected]'); 
Route::get('/delete/{task}' , '[email protected]'); 
Route::post('/delete', '[email protected]'); 

Route::get('/task/{id}' , '[email protected]')->where('id', '\d+'); 

}); 

這是我HomeController.php;

class HomeController extends BaseController { 

public function showLogin() 
{ 
    return View::make('login'); 
} 
public function doLogin() 
{ 
    $userdata = array(
     'username' => Input::get('username'), 
     'password' => Input::get('password') 
    ); 
    dd(Auth::attempt($userdata)); 

    if(Auth::attempt($userdata)) 
    { 

     return Redirect::to('/'); 
    } 
    else 
    { 
     return Redirect::to('login'); 
    } 
} 

public function doLogout() 
{ 
    Auth::logout(); 
    return Redirect::to('login'); 
} 

} 

,這是我login.blade.php文件:

@extends('layout') 
@section('content') 
<section class="header section-padding"> 
<div class="background">&nbsp;</div> 
<div class="container"> 
    <div class="header-text"> 
     <h1>Learning Laravel: The Easiest Way</h1> 
     <p> 
      Showing a single task <br/> using route parameter! 
     </p> 
    </div> 
</div> 
</section> 
<div class="container"> 
<section class="section-padding"> 
    <div class="jumbotron text-center"> 
     <h1> 
      Login 
     </h1> 
     <P> 
      {{ $errors->first('username') }} 
      {{ $errors->first('password') }} 
     </P> 

     {{ Form::open(['url' => '/login', 'class' => 'form']) }} 
     <div class="form-group"> 
      {{ Form ::label('username', 'Username:') }} 
      {{ Form::text('username')}} 
     </div> 
     <div class="form-group"> 
      {{ Form::label('password', 'Password:') }} 
      {{ Form::password('password') }} 
     </div> 
     <div class="form-group"> 
      {{ Form::submit('Login', ['class' => 'btn btn-primary']) }} 
     </div> 
     {{ Form::close() }} 
    </div> 
</section> 
</div> 
@stop 

當我輸入任何用戶名和密碼,我沒有錯誤,我從來沒有登錄,我重定向到登錄頁面和DD( )始終返回布爾(假的),任何一個可以幫助這一點,解釋有關身份驗證的Laravel更多,感謝ü:)

編輯

這是我的模型/ user.php的,我不任何代碼添加到這樣的:

class User extends Eloquent implements UserInterface, RemindableInterface { 

    use UserTrait, RemindableTrait; 


    protected $table = 'users'; 

    protected $hidden = array('password', 'remember_token'); 

} 

創建我用戶表手動

+0

你能過去你的用戶模型代碼嗎? – 2014-11-02 15:42:13

+0

如何保存用戶? – 2014-11-02 16:01:38

+0

@KalhanoToressPamuditha我認爲我們需要保存用戶,如果只有我們想創建一個用戶,但在這裏我想檢查一個用戶是否存在數據庫或不 – Arman 2014-11-02 16:02:55

回答

1

Auth::attempt($userdata)方法將散列$userdata陣列password,並檢查與密碼散列數據庫值,

,所以你需要散列密碼的數據庫,

驗證,

請更改數據庫中的密碼$2y$10$3S3yDwfkwwLghedu4AoaTe//61QTaNC0ycTdp8hLfHtQS4XrgBPQy,並使用a的密碼字段的形式

$2y$10$3S3yDwfkwwLghedu4AoaTe//61QTaNC0ycTdp8hLfHtQS4XrgBPQya的laravel哈希密碼。