Laravel 4.2 auth:尝试失败

时间:2014-09-13 19:27:02

标签: authentication laravel

我无法找到这个网站,但我无法弄清楚我的代码有什么问题。

我想为true获取var_dump(Auth::attempt()),但始终是false。 这是我的控制器方法:

public function vartotojoPrisijungimoForma(){
    $view = View::make('vartotojai.vartotojoPrisijungimoForma',
    array('title'=>'Vartotojo Prisijungimas'));

    var_dump(Auth::attempt(array('vardas'=>'aaaaa','pw'=>'aaaaa')));

    return $view;
}

在我的数据库中,用户名存储为vardas,密码存储为pw

我的auth.php文件如下所示:

<?php

return array(
    'driver' => 'eloquent',
    'model' => 'Vartotojai',
    'table' => 'vartotojai',

    'reminder' => array(
        'email' => 'emails.auth.reminder',
        'table' => 'password_reminders',
        'expire' => 60,
),

);

Vartotojai.php的模型文件如下所示:

<?php 
    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableInterface;

class Vartotojai extends ModeliuBaze implements UserInterface, RemindableInterface{
    protected $table = 'vartotojai'; 
    public $timestamps = false; 

    protected static $rules = array( 
        'vardas'=>'required|unique:vartotojai|min:4|regex:/[a-zA-Z]$/',
        'pw'=>'required|alpha_num|between:4,8|confirmed',
        'pw_confirmation'=>'required|alpha_num|between:4,8'
    );

    protected static $messages = array( 
        'required' => ':attribute laukelis tuscias!',
        'min'=> ':attribute laukelyje galimas minimalus(:min) simboliu kiekis!',
        'between' => ':attribute laukelis gali buti nuo :min - :max simboliu intervale!',
        'vardas.regex'=> ':attribute turi atitikti siuos simbolius (a-zA-Z)',
        'unique'=> 'Jau vartotojas su tokiu vardu uzregistruotas!',
        'alpha_num'=>':attribute laukelyje galima rasyti tik skaicius ir raides!',
        'confirmed'=>'Nesutampa slaptazodziai!'
    );

    protected $hidden = array('password');

    public function getAuthIdentifier(){
        return $this->getKey();
    }
    public function getAuthPassword(){
        return $this->password;
    }
    public function getRememberToken(){
        return $this->remember_token;
    }
    public function setRememberToken($value){
        $this->remember_token = $value;
    }
    public function getRememberTokenName(){
        return 'remember_token';
    }
    public function getReminderEmail(){
        return $this->email;
    }
    }

我试图检查哈希:

    public function vartotojoPrisijungimoForma(){
        $view = View::make('vartotojai.vartotojoPrisijungimoForma',
        array('title'=>'Vartotojo Prisijungimas'));
        $pw = Vartotojai::find('5');

        var_dump(Auth::attempt(array('vardas'=>'aaaaa','pw'=>'aaaaa')));
        var_dump(Hash::check('aaaaa',$pw->pw));
        return $view;
        }

哈希检查显示为真。

1 个答案:

答案 0 :(得分:0)

您需要在数据库中将密码字段从“密码”更改为“密码”。 Laravel使用“密码”验证凭据。

OR

您可以将此功能更改为:

public function getAuthPassword(){
    return $this->pw;
}

然后你的尝试功能应该是:

var_dump(Auth::attempt(array('vardas'=>'aaaaa','password'=>'aaaaa')));