R
R
Romi2021-11-05 23:15:37
Laravel
Romi, 2021-11-05 23:15:37

Can anyone explain the sample closure from the docs on creating a custom guard for Auth?

I decided to remake the custom guard from anonymous (Auth::viaRequest) to normal, but I don’t understand the sample in the dock:

Auth::extend('jwt', function ($app, $name, array $config) {
            // Возвращаем экземпляр `Illuminate\Contracts\Auth\Guard` ...
            return new JwtGuard(Auth::createUserProvider($config['provider']));
        });

what is this $app, $name ?

why do you need to pass Auth::createUserProvider($config['provider']) to the Guard constructor?

Is it possible to do without these difficulties somehow? )))

let's say I wrote the Guard implementation in the form of MyGuard, then I can do this:
Auth::extend('myguard', function() {
            return new MyGuard();
        });

and if not, why not?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Romi, 2021-11-06
@romicohen

By the method of scientific poke, it was found that:

Auth::extend('myguard', function() {
            return new MyGuard();
        });

does work :) However, if you pass $app in the arguments, then the entire application container will be available, and yes (maybe it will come in handy for someone), Request is available via $app['request'] :-)
And yes, good people wrote a couple for such cases cheat sheets:
Illuminate/Auth/TokenGuard.php
and
https://github.com/irazasyed/jwt-auth-guard/tree/m...
:-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question