robsontenorio / laravel-keycloak-guard

🔑 Simple Keycloak Guard for Laravel
MIT License
434 stars 141 forks source link

If load_user_from_database' is set to false, there is no way to get user info from db ? #108

Open raj-saroj-vst-au4 opened 8 months ago

raj-saroj-vst-au4 commented 8 months ago

If load_user_from_database is set to false, and i try auth()->user() it throws an empty array.

Is there any specific method available that i can call to get the user data from db ? my flow is as follows :- the user authenticates through keycloak > is forced on to signup page(due to a middleware that checks for user auth and signupcomplete column) > user signs up > records in db and sets signupcomplete to 1

now since i have set the load_user_from_database to false how do i get user info from db like auth()->user() or any other way ?

robsontenorio commented 8 months ago

If you set it to “false” of course this package won’t try to load the user from the database.

So , you need to do it by yourself using some middleware

raj-saroj-vst-au4 commented 8 months ago

but bro if the user is logging in for the first time, how would db contain user data to be able to pull his info ? and we cant even have an unprotected api route cause of security issues.

all am asking for is a feature wherein i could define which routes i want to just authenticate(for initial signup api) and all other routes on which i want to have retrieveByCredentials or load_from_db to be true

raj-saroj-vst-au4 commented 8 months ago

can u make this method more modular KeycloakGuard::class->validate()

so that i can call it explicitly ? like Auth::validate($credentials, false)

where second parameter is a boolean and would define whether to load user from db or not !

for example

$loadfromdb = $this->config['load_user_from_database'];

public function validate(array $credentials = [], $loadfromdb = true)
    {
        $this->validateResources();

        if ($loadfromdb) {
            $methodOnProvider = $this->config['user_provider_custom_retrieve_method'] ?? null;

            if ($methodOnProvider) {
                $user = $this->provider->{$methodOnProvider}($this->decodedToken, $credentials);
            } else {
                $user = $this->provider->retrieveByCredentials($credentials);
            }

            if (!$user) {
                throw new UserNotFoundException("User not found. Credentials: ".json_encode($credentials));
            }
        } else {
            $class = $this->provider->getModel();
            $user = new $class();
        }

        $this->setUser($user);

        return true;
    }
robsontenorio commented 8 months ago

Take a look at “user_provider_custom_retrieve_method” on README.

raj-saroj-vst-au4 commented 8 months ago

Yeah i read that but, that part would execute only if the load_user_from_db is true…