2014-11-02 179 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哈希密码。