为什么我自己的身份验证不适用于Laravel中我自己的模型?

时间:2014-09-24 19:58:59

标签: php authentication laravel-4

我正在使用Laravel 4.2,而我正试图创建自己的模型(我不使用User模型)。 传递邮件和密码时会出现问题,然后我使用方法Auth::attempt输入其他(它对应于错误)

Usuario Controller

class UsuarioController extends BaseController{

function doLogin(){
    $userdata = array(
        'Correo' => Input::get('correo'),
        'Contrasena' => Input::get('contrasena')
        );
    if(Auth::attempt($userdata)){
        echo 'SUCCESS!';

    }else{
        echo 'Error!';

    }
} ...

Usuario模型

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Usuario extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'Usuario';
protected $primaryKey = 'idUsuario';
protected $fillable = array(
                        'Nombre',
                        'Apellido',
                        'Rol',
                        'Correo',
                        'Contarsena',
                        'Cumpleanos',
                        'Foto', 
                        'Pais',
                        'Comuna',
                        'Profesion_idProfesion',
                        'Institucion_idInstitucion',
                        'remember_token'
                    );

function profesion(){
    return $this->belongsTo('Profesion', 'idProfesion');
}


public function getPasswordAttribute()
{
    return $this->Contrasena;
}

public function setPasswordAttribute($Contrasena)
{
    $this->Contrasena= $Contrasena;
}   

public function getReminderEmail()
{
    return $this->Correo;
}    

public function getRememberToken()
{
    return $this->remember_token;
}

public function setRememberToken($value)
{
    $this->remember_token = $value;
}

public function getRememberTokenName()
{
    return 'remember_token';
}
public function getAuthIdentifier()
{
    return $this->getKey();
}
public function getAuthPassword() {
    return $this->Contrasena;
}       

}

Auth.php

return array(

'driver' => 'eloquent', //database or eloquent

'model' => 'Usuario',

'table' => 'Usuario',

'username' => 'Correo',
'password' => 'Contrasena',

'reminder' => array(

    'email' => 'emails.auth.reminder',

    'table' => 'password_reminders',

    'expire' => 60,

),

);

Usuario Table

enter image description here

应用程序永不崩溃,但在如果条件中,请始终输入''否则''返回错误!

2 个答案:

答案 0 :(得分:0)

尝试

dd(DB::getQueryLog());

获取执行的SQL。这使故障排除更容易。

我的猜测是,没有密码'字段,尝试方法将自动哈希

答案 1 :(得分:0)

您的可填充数组中有拼写错误:

protected $fillable = array(
                    'Nombre',
                    'Apellido',
                    'Rol',
                    'Correo',
                    'Contarsena',
                    'Cumpleanos',
                    'Foto', 
                    'Pais',
                    'Comuna',
                    'Profesion_idProfesion',
                    'Institucion_idInstitucion',
                    'remember_token'
                );

Contarsena 应为 Contrasena

您的身份验证阵列应包含emailpassword密钥:

$userdata = array(
    'correo' => Input::get('correo'),
    'password' => Input::get('contrasena')
);