0
votes

I had tried some solutions mentioned in this platform but every time was failed. My Laravel app has authentication function and users can login with email or id with password. This is working well but "remember me" function is not working.

MySQL 'users' table has 'remember_token' field and it fills token value into the field when I try to login with "remember me" checkbox checked.

enter image description here

And 'attempt' function is also called with boolean value as second parameter.

protected function attemptLogin(Request $request)
{
    Log::info($request); // 'remember' => 'on',
    return $this->guard()->attempt(
        $this->credentials($request), $request->filled('remember')
    );
}

Following image is of first login. enter image description here

If I click logout it redirects to login page again and it looks like below. enter image description here

Please help me solving this problem. Thank you in advance.

[EDIT] Appended some code in login.blade.php file.

    @csrf
<div class="form-group row">
    <label for="identity" class="col-md-4 col-form-label text-md-right">メール または ID</label>

    <div class="col-md-6">
        <input id="email" type="identity" class="form-control @error('email') is-invalid @enderror" name="identity" value="{{ old('email') }}" required autocomplete="email" autofocus>

        @error('email')
            <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>
</div>

<div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">パスワード</label>

    <div class="col-md-6">
        <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

        @error('password')
            <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>
</div>

<div class="form-group row">
    <div class="col-md-6 offset-md-4">
        <div class="form-check">
            <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

            <label class="form-check-label" for="remember">
                次回から自動ログインする
            </label>
        </div>
    </div>
</div>

PS: I installed voyager in laravel app.

1
Tested in chrome, firefox and edge. but no luck - Jihun
Please can you show the code in your login.blade.php file. - Rwd

1 Answers

1
votes

You need to pass some $remember information at the Auth::attempt

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
    // The user is being remembered...
}

If you would like to provide "remember me" functionality in your application, you may pass a boolean value as the second argument to the attempt method, which will keep the user authenticated indefinitely, or until they manually logout. Of course, your users table must include the string remember_token column, which will be used to store the "remember me" token.

https://laravel.com/docs/5.5/authentication#remembering-users