Auth::getRecallerName() giving different remember token name in Laravel 9

132 views Asked by At

I am implementing Laravel Remember Me functionality in my Laravel App.

I am using two separate guards:-

  1. Guard 'admin' for admin panel.
  2. Guard 'user' for front-end site.

Here is my code in config/auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],

    'user' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

Guard 'web' isn't being used.

This was my initial code in LoginController.php :-

$auth = auth()->guard('admin')->attempt(array(
                                        'email'     => $email,          // match email                              
                                        'password'  => $password        // match password
                                    ), $request->has('remember') ? true : false);                                 // attempt the authentication
if($auth)
{  
    // if authentication is success, get redirected to the dashboard 
    return Redirect::Route($data['entity']. '.dashboard');
}
else
{   
    // if authentication fails, get redirected to the login page with error message 
    $request->session()->flash('error','Invalid Password');
    return Redirect::Route($data['entity'] . '.' . $data['action']);
}

As you can see, the Remember Cookie has been created with name [remember_admin_.......]

enter image description here

Now, I wanted to change the expiry of the cookie. I read this write-up and tried to implement the same. Here is my app/Http/Traits/RememberMeTrait.php file code:-

<?php
namespace App\Http\Traits;
use Auth, Cookie, Crypt;

trait RememberMeTrait
{
    /****************************************************/
    # Purpose          : Seting Up Epiration       
    /****************************************************/
    protected $minutesExpiration = 1; //30 days = 43200 minutes; 1 day = 1440 minutes

    /***********************************************************/
    # Customize the user logged remember me expiration 
    # 
    # @param  \Illuminate\Contracts\Auth\Authenticatable  $user
    /***********************************************************/
    public function setRememberMeExpiration($user) 
    {
        $rememberMeSessionName  = $this->getRememberMeSessionName($user);
        $rememberMeValue        = $this->setRememberMeValue($user); 
        Cookie::queue($rememberMeSessionName, $rememberMeValue, $this->minutesExpiration);
    }

    /***********************************************************/
    # Generate remember me value
    #
    # @return string
    /***********************************************************/
    protected function setRememberMeValue($user) 
    {
        $tokenString = $user['_id'] . "|" . $user['email'];
        return Crypt::encryptString($tokenString);
    }

    /***********************************************************/
    # Get remember me session name
    #
    # @return string
    /***********************************************************/
    protected function getRememberMeSessionName($user) 
    {
        return Auth::getRecallerName();
    }
}

Then made the modifications in the LoginController.php file like this:-

use App\Http\Traits\RememberMeTrait;

class LoginController extends Controller
{
    use RememberMeTrait;
    ------------------------
    -----------------------

    $auth = auth()->guard('admin')->attempt(array(
                                            'email'     => $email,          // match email                              
                                            'password'  => $password        // match password
                                        ), $request->has('remember') ? true : false);                                 // attempt the authentication
    if($auth)
    {  
        // if authentication is success, get redirected to the dashboard 
        if($request->has('remember')) {
            $this->setRememberMeExpiration($userDetail);
        }
        return Redirect::Route($data['entity']. '.dashboard');
    }
    else
    {   
        // if authentication fails, get redirected to the login page with error message 
        $request->session()->flash('error','Invalid Password');
        return Redirect::Route($data['entity'] . '.' . $data['action']);
    }
    -------------------
    -------------------
}

This, however, instead of setting up the remember_admin....... cookie, is creating a new cookie remember_web....... with the expiry as 1 minute.

enter image description here

How can I fix this?

1

There are 1 answers

1
Shyam On

Try with this update code @LoginController.php

 if ($auth) {  
        if ($request->has('remember')) {
            $this->setRememberMeExpiration(auth()->guard('admin')->user());
        }
        return Redirect::Route($data['entity'] . '.dashboard');
    } else {
        // ..
    }