验证用户Laravel

时间:2014-11-02 13:24:22

标签: php authentication laravel laravel-4

大家好我有一个简单的laravel应用程序,我尝试将用户身份验证添加到我的应用程序,这是我的route.php文件:

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


Route::get('/login', 'HomeController@ShowLogin');
Route::post('/login', 'HomeController@doLogin');

Route::get('/logout' , 'HomeController@doLogout');


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

Route::get('/', 'TasksController@home');
Route::get('/create', 'TasksController@create');
Route::get('/edit/{task}', 'TasksController@edit');
Route::post('/edit', 'TasksController@doEdit');
Route::post('/create' , 'TasksController@saveCreate');
Route::get('/delete/{task}' , 'TasksController@delete');
Route::post('/delete', 'TasksController@doDelete');

Route::get('/task/{id}' , 'TasksController@show')->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()总是返回bool(false),任何人都可以帮助,并解释更多关于Laravel中的身份验证,谢谢你啊:)

修改

这是我的模型/ User.php,我不添加任何代码:

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;


    protected $table = 'users';

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

}

我手动创建用户表

1 个答案:

答案 0 :(得分:1)

Auth::attempt($userdata)方法将对password数组中的$userdata进行哈希处理,并使用数据库值检查哈希密码,

所以你需要在数据库中使用散列密码,

验证,

请将数据库中的密码更改为$2y$10$3S3yDwfkwwLghedu4AoaTe//61QTaNC0ycTdp8hLfHtQS4XrgBPQy,并使用a作为

形式的密码字段

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