【问题标题】:Hash::check() works but Auth::attempt fails (custom User model)Hash::check() 有效,但 Auth::attempt 失败(自定义用户模型)
【发布时间】:2015-02-18 18:31:51
【问题描述】:

我正在学习 Laravel 4,但现在我无法让用户登录我的应用程序。 我阅读了多个教程来创建登录区域,但我无法创建会话。

解决方案: laravel 4 custom named password column

您必须小心“密码”索引,无论您的表如何命名密码字段,它必须是 Auth::attempt 函数中的“密码”

用户数据库表:

  • idusuario, int(11)
  • 登录,varchar(45)
  • 名词,varchar(45)
  • 对比,varchar(255)
  • 状态,布尔值
  • remember_token, varchar(100)
  • idperfil, int(11)

这是我的认证功能:

Route::post('login', function()
{
    if(Auth::attempt([ 'login' => Input::get('login'), 'contrasena' => Input::get('password') ]))
        return Redirect::intended('home');
    return Redirect::to('login')->with('response','Ingreso invalido');
}

我的自定义用户模型:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Usuario extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'usuario';
    protected $hidden = array('contrasena');
    protected $fillable = array('login', 'nombre', 'status', 'idperfil');
    protected $guarded = array('idusuario', 'contrasena');
    public $primaryKey = 'idusuario';
    public $timestamps = false;

    public function getAuthIdentifier()
    {
        return $this->login;
    }

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

    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 false;
    }

我的看法:

{{ Form::open(array('url' => 'login', 'role' => 'form')) }}
    <div class="response">{{ $response or '' }}</div>
    <div class="form-group">
        <label for="login">Usuario</label>
        <input type="text" class="form-control" id="login" name="login" placeholder="Ingrese su login">
    </div>
    <div class="form-group">
        <label for="password">Contraseña</label>
        <input type="password" class="form-control" id="password" name="password" placeholder="*********">
    </div>
    <button type="submit" class="btn btn-primary">Iniciar Sesión</button>
</form>

我一直在测试

dd(Auth::attempt([ 'login' => Input::get('login'), 'contrasena' => Input::get('password') ]));

但我无法修复它,但是当我测试哈希检查以验证密码时,它返回 true! 有什么想法吗? 提前致谢!

【问题讨论】:

标签: session authentication laravel login login-attempts


【解决方案1】:

对不起,我没有看第二部。 Jarek Tkaczyk 在this post 中的回答行。 如果您有同样的问题,请务必查看您的索引名称。

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 2020-02-14
    • 2016-09-27
    • 2015-12-14
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2017-04-28
    相关资源
    最近更新 更多