Laravel 5.8 : Remember me token not being saved in database

北城以北 提交于 2020-01-25 10:04:12

问题


The token is created as a cookie, I can see it in my browser, but it's not being saved in the database.

Here is my User model :

<?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',
    ];
}

My login method in my 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('/');
    }

When I check or not the remember_me option, it uses the Auth::login() method accordingly, with true or false argument. However, in my database, the field is never set, although I can see a token in my browser remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d.

The field in my db : remember_token varchar(100) null,


回答1:


You should add remember_token as varchar(100) in your accounts' table.




回答2:


Ok, I found the answer while searching for another problem.

It was due to my id field in my accounts table not being named 'id' in lowercase. My field was 'Id'. (Not my choice, I know it's bad).

To fix it, I had to add : protected $primaryKey = 'Id'; to my User model.



来源:https://stackoverflow.com/questions/57768735/laravel-5-8-remember-me-token-not-being-saved-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!