【问题标题】:Why my own authentication doesn't work with my own model in Laravel?为什么我自己的身份验证不适用于我自己在 Laravel 中的模型?
【发布时间】:2014-09-29 10:27:10
【问题描述】:

我正在使用 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!';

    }
} ...

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 表

应用程序永远不会崩溃,但在 If Condition 总是输入 ''else'' 返回 Error!

【问题讨论】:

    标签: php authentication laravel-4


    【解决方案1】:

    您的可填充数组中有错字:

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

    Contarsena 应该是 Contrasena

    您的身份验证数组应包含email 和password 密钥:

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

    【讨论】:

    • 如果我这样做会返回Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from `Usuario` where `email` = marcelo@example.com limit 1),所以我将email 替换为Correo 并且可以工作:D
    【解决方案2】:

    试试

    dd(DB::getQueryLog());
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2017-06-28
      • 2023-03-12
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      相关资源
      最近更新 更多