-1

I want to rewrite onceBasic() of Illuminate\Auth\SessionGuard by extending Facades\Auth, but encountering the problem of not being able to access the helper protected function of SessionGuard class. Here is how I extends Auth facades:

namespace App\MyFacades;
use \Illuminate\Support\Facades\Auth;

class Auth_QuyLe extends Auth {

        /**
         * Create your custom methods here...
         */
        public static function onceBasic_QuyLe($field = 'name', $extraConditions = [])
        {   
            $credentials =  self::basicCredentials(self::getRequest(), $field);

            if (! self::once(array_merge($credentials, $extraConditions))) {
                return self::failedBasicResponse();
            }
        }

}

When I call onceBasic_QuyLe() from my middleware, it shows

"Method Illuminate\Auth\SessionGuard::basicCredentials does not exist."

I've already updated my class to config/app.php and run composer dump-autoload && php artisan config:cache

free2idol1
  • 174
  • 1
  • 3
  • 12
  • Don't try to make any changes to a Facade. Facades simply resolve an instance of the underlying class which in this case is the currently used Auth guard (SessionGuard). – Rwd May 11 '21 at 11:51
  • No... I don't changes Facades... I don't modify a single code of laraval core... What I did is to "extend it", creating my own function, utilizing some helpers from Auth Facades... Here are some similar posts in SO: https://stackoverflow.com/questions/40614875/laravel-5-extend-a-facade | https://stackoverflow.com/questions/16953460/how-to-extends-a-facade-in-laravel | https://stackoverflow.com/questions/52944843/extending-db-facade-laravel I am not sure why in my situation, I just can't access the protected function of Auth facades, although public function is fine... – free2idol1 May 11 '21 at 14:39
  • Sorry, I wasn't trying to suggest you were editing anything in the core. What I meant was extending the Facade itself isn't the answer. For the most part think of a Facade like a helper function e.g. `auth()->user()` and `Auth::user()` will call the same method because they're actually delegating the call to the underlying `Guard` class. Realistically, you need to extend the underlying `Guard` class and have Laravel use that instead. – Rwd May 11 '21 at 17:09
  • Anyway, what version of Laravel are you using? – Rwd May 11 '21 at 17:12
  • OK...Thanks for your suggestion... I just tried out extending the underlying Guard class: ` – free2idol1 May 12 '21 at 04:43
  • https://gist.github.com/wayanjimmy/afa85e211e2cdd6bacf5daa849bac749 – Rwd May 12 '21 at 12:53

1 Answers1

0

I've finally found a solution:

Firstly, b/c SessionGuard is a “macroable” class, and if you need to add an additional method to SessionGuard , you need to go to AppServiceProvider and declare macros into the service provider’s boot method

public function boot()
    {
        SessionGuard::macro('myOnceBasic', function ($field = 'name', $extraConditions = []) {

            $credentials = $this->basicCredentials($this->getRequest(), $field);

            if (!$this->once(array_merge($credentials, $extraConditions))) {
                return$this->failedBasicResponse();
            }
        });
}

and in the underlying class that your newly created Facades represents , you can use Auth facade to call the macros:

class MySessionGuard{

        public function myOnceBasic($field = 'name', $extraConditions = [])
        {   
            Auth::myOnceBasic($field = 'name', $extraConditions = []);
        }

}
free2idol1
  • 174
  • 1
  • 3
  • 12