mariovalney / laravel-keycloak-web-guard

Simple Keycloak Guard to Laravel Web Routes - https://packagist.org/packages/vizir/laravel-keycloak-web-guard
146 stars 80 forks source link

Is it possible to Save the User to the database? #11

Closed bim-and-more-inikolov closed 4 years ago

bim-and-more-inikolov commented 4 years ago

Hello,

How can I save the Model when the user is authenticated to the database?

Thank you in advance!

Best, Ivan

mariovalney commented 4 years ago

You can use the data retrieved by Keycloak to create a user.

And you can implement your own user model: https://github.com/Vizir/laravel-keycloak-web-guard#how-to-implement-my-model

bim-and-more-inikolov commented 4 years ago

Dear Mario,

Thanks for the fast answer. I already created my own model. My question is more about where in the code I should save the user? Is it in the constructor of my new User model?

Thanks in advance!

mariovalney commented 4 years ago

There is no hook on Guard to do it. :(

You can do it in user construct... maybe (or in controller). But I'm not sure it is the best place, because will save every request...

Maybe a better solution is:

1 - In config leave "callback" route empty.

This way it will not be registered here.

2 - Next register your own route.

This way:

// Should be on middleware web and keep this name
Route::get('callback', 'YourController@callback')->name('keycloak.callback');

3 - Process the callback.

This controller should extend Illuminate\Routing\Controller and implemente the callback method from Vizir\KeycloakWebGuard\Controllers\AuthController (check here).

4 - Now you can save User after login callback

On callback, right before redirect to intended page.

On previous step, you can extend the AuthController and call parent callback saving the response. It's better because you'll get the changes from our controller (we have nothing planned for this, but maybe...).

After that, check user is setted, save and return parent response.


Remember: you can get tokens and profile using KeycloakService or the facade:

use Vizir\KeycloakWebGuard\Facades\KeycloakWeb;

I hope this help you :)

mariovalney commented 4 years ago

Hey @bim-and-more-inikolov . It did work?

mariovalney commented 4 years ago

Closing due to inactivity.

gpedro commented 4 years ago

check my solution #32