【问题标题】:Laravel 5.8 : Remember me token not being saved in databaseLaravel 5.8:记住我的令牌没有保存在数据库中
【发布时间】:2019-09-03 09:23:57
【问题描述】:

令牌被创建为 cookie,我可以在浏览器中看到它,但它没有保存在数据库中。

这是我的用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    public $timestamps = false;

    protected $table = 'accounts';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'Username', 'email', 'Password', 'Nickname', 'SecretQuestion', 'SecretAnswer', 
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'Password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

我的 LoginController 中的登录方法:

public function login(Request $request)
    {
        $user = User::where('Username', $request->Username)
                    ->where('Password', md5($request->password))
                    ->first();
        if (is_null($user)) return back()->with('error', 'Votre identifiant ou mot de passe est incorrect.');
        Auth::login($user, $request->remember == 'on' ? true : false);
        if (Auth::check()) return Redirect::to('/');
    }

当我检查或不检查 remember_me 选项时,它会相应地使用 Auth::login() 方法,并带有 true 或 false 参数。 但是,在我的数据库中,该字段从未设置过,尽管我可以在浏览器中看到一个令牌remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d。

我的数据库中的字段: remember_token varchar(100) null,

【问题讨论】:

  • 你为什么要md5输入密码?您使安全登录不安全。
  • 我为其开发网站的应用程序使用 md5 身份验证。我别无选择。

标签: php laravel token remember-me


【解决方案1】:

好的,我在寻找另一个问题时找到了答案。

这是由于我的帐户表中的 id 字段没有以小写形式命名为“id”。 我的领域是“Id”。 (不是我的选择,我知道这很糟糕)。

要修复它,我必须在我的用户模型中添加:protected $primaryKey = 'Id';。

【讨论】:

    猜你喜欢
    • 2014-12-19
    • 2019-10-19
    • 2020-12-24
    • 2021-08-12
    • 2020-01-15
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多