繁体   English   中英

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

[英]Why my own authentication doesn't work with my own model in Laravel?

我正在使用Laravel 4.2,并且正在尝试认证自己的模型(我不使用User模型)。 通过邮件和密码时出现问题,然后我使用方法Auth::attempt输入到else(它对应于错误)

Usuario控制器

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!';

    }
} ...

乌苏里奥模型

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,

),

);

乌苏里奥表

在此处输入图片说明

该应用程序永远不会崩溃,但如果始终在“ 条件”中输入“ else”,则返回错误!

尝试

dd(DB::getQueryLog());

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

我的猜测是没有“ password”字段,尝试方法会自动将其散列

您的可填写数组中有一个错字:

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')
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM