larastan icon indicating copy to clipboard operation
larastan copied to clipboard

Auth facade doesn't recognize macros registered on SessionGuard,RequestGuard

Open notdian opened this issue 3 years ago • 5 comments

  • Larastan Version: 9.27.0
  • --level used: 8

Description

Call to an undefined method Illuminate\Support\Facades\Auth::authed().  

Laravel code where the issue was found

$authedMacro = function (): Model {
    if (($auth = Auth::user()) !== null) {
        return $auth;
    }

    throw new UnauthorizedException();
};

SessionGuard::macro('authed', $authedMacro);
RequestGuard::macro('authed', $authedMacro);

notdian avatar Sep 04 '22 18:09 notdian

Thank you for your report! 👋🏻

What happens if you temporarily try this?

SessionGuard::macro('authed', function (): Model {
    if (($auth = Auth::user()) !== null) {
        return $auth;
    }

    throw new UnauthorizedException();
});

szepeviktor avatar Sep 04 '22 18:09 szepeviktor

Hello there,

I get the same error.

notdian avatar Sep 04 '22 18:09 notdian

Could you share the code where this error happens?

szepeviktor avatar Sep 04 '22 18:09 szepeviktor

Inside OrderController

$user = Auth::authed()

this happens with 7 of my macros auth macros.

if I switch to Auth::guard()->authed() it works as GuardDynamicStaticMethodReturnTypeExtension returns the correct guard, AuthManager's __call forwards the call to the current guard.

notdian avatar Sep 04 '22 18:09 notdian

If I understood it correctly, this bug is caused by #1295. I proposed solutions to this in #1315. Second solution, rollback the PR and fix that issue in some other way, will fix this issue as well. After rollback the logic should be something like this:

  1. Pipes\Facades will get underlying class of facade Auth and send it back to pipeline
  2. Pipes\Auths will send Guard contract to pipeline
  3. Pipes\Contracts will resolve Guard contract to default guard
  4. Pipes\Macros will find macro function inside default guard

Right now step 4 is missing because check for macros was moved to separate extension.

xwillq avatar Sep 06 '22 13:09 xwillq

Can you guys try the latest master branch and see if it fixes the issues?

canvural avatar Oct 11 '22 10:10 canvural

Fixed with 6612298

canvural avatar Oct 12 '22 09:10 canvural