imanghafoori1 / laravel-MasterPass

Helps you securely setup a master password and login into user accounts with it.
MIT License
364 stars 29 forks source link

fix Method Illuminate\Auth\RequestGuard::isLoggedInByMasterPass does not exist #47

Closed andrii-trush closed 1 year ago

andrii-trush commented 1 year ago

As soon as project uses Jetstream + Inertia there is an error Method Illuminate\Auth\RequestGuard::isLoggedInByMasterPass does not exist. in Laravel 10. Bug was detected in middleware.

RequestGuard implements Guard. Not StatefulGuard.

Was tested on Laravel Framework 10.14.1 and PHP 8.1.20

imanghafoori1 commented 1 year ago

Thanks @andrii-trush for your PR but I do not think this fix is backward compatible, is it?

andrii-trush commented 1 year ago

@imanghafoori1 interface StatefulGuard extends from interface Guard

Here are an example in PHP sandbox https://onlinephp.io/c/99b33 with proof of backward compatible

Looks like here is an issue of this problem. https://github.com/imanghafoori1/laravel-MasterPass/issues/28

imanghafoori1 commented 1 year ago

can we do it like this:

$authGuard = Auth::guard($guard);
$stateful = class_exists(StatefulGuard::class) && $authGuard instanceof StatefulGuard;

if ( $stateful || $authGuard  instanceof Guard) {
    //  ...
}

what has happened in laravel 10 which breaks this?

andrii-trush commented 1 year ago

@imanghafoori1 As soon as I'm making check for both interfaces, PHPStorm shows this warning

image

Here are my middleware where i receive an error

 if (Auth::guard('sanctum')->check() && ! Auth::isLoggedInByMasterPass()) {
            $request->user()->update([
                'last_activity_at' => Carbon::now(),
            ]);
        }

In this case Auth is instance of \Illuminate\Auth\RequestGuard and RequestGuard implements Guard.

As mentioned early about StatefulGuard here are a code from laravel/framework:

namespace Illuminate\Contracts\Auth;

interface StatefulGuard extends Guard

So my fix is safe

imanghafoori1 commented 1 year ago

I just merged it for now, but since I do not have time to fully test it. I do not release a new version. Let's see if I get the time soon. So you are free to install the dev-master version.

imanghafoori1 commented 3 weeks ago

@andrii-trush Can you use the isLoggedInByMasterPass macro with the Request guard?