auth0 / laravel-auth0

Laravel SDK for Auth0 Authentication and Management APIs.
MIT License
246 stars 135 forks source link

Docs Question: Stateful Repository, where to create the user? #400

Closed shealavington closed 1 year ago

shealavington commented 1 year ago

What should be added?

I believe this ticket is regarding documentation and difficulty in finding a clear path.

My team and I are looking at implementing a custom respository so we can implement our own users table in our database.

The question...

We have a stateful session for our application. I am wondering, when we succeed from an Auth0 login, should we perform our User::createOrUpdate(...) in the UserRepository fromSession or from a LogSuccessfulLogin event listener handler?

My Assumptions

One one hand, it appears we should create and update the user after a login request (Event Handler), and the fromSession would be there to fetch the user data from the database for subsequent checks. On the other hand, it looks like the UserRepository is there to both fetch and create models suggesting it should all be done there.

Though I believe the second solution is messy, due to the issue mentioned in the linked comment where on the first hit sub is defined, but subsequent hits, it is not : https://github.com/auth0/laravel-auth0/issues/385#issuecomment-1559169521

The confusion

The documentation for the UserRepository fromSession implies it is there to "construct a model" from scratch. But the event handler docs also suggests it's there so you can "update a database" after successful login.

I'm not sure where is best to perform my create and fetch actions.

Thanks, appreciate the time, hopefully we can clear this one up quickly 😅

evansims commented 1 year ago

Hi, @shealavington! 👋 Thanks for your question, and very sorry for the delay, I'm on holiday at the moment.

The event handler is provided as a convenience for potential manipulation of user models if desired, but all the real retrieval and storage operations should happen within the UserRepository. I can certainly see why this would be confusing, and I'll work on updating the docs to bring clarity there, but, you have the right of it, fromSession is where you want to perform those operations.

Thanks!

shealavington commented 1 year ago

Perfect, thank you, no problem, enjoy your holiday! ☀️

Is there anything that can be done to try and make the functions more single purpose?

As I referenced, a few of us have now had to do a hacky looking isset($user['sub']) to determine if the data is from auth0, or our already stored model.