In this episode, we dive into refactoring our authentication flow to make it cleaner and more maintainable by introducing a factory for creating users from social providers (like Twitter and GitHub).
Starting off, we realize that instead of returning simple strings, our factory should return classes responsible for actually creating user models for each provider. We structure our app a bit by setting up an Actions/Social
folder, and in it we create a new CreateXUser
class, which will handle user creation logic. To keep things consistent and extensible, we make a contract (an interface) that all of these user creation actions should follow, ensuring each will have a create
method that takes a Socialite user and spits out our User model.
We move our actual user-creation logic out of the controller and into this new class, making sure the flow is now: when a user logs in via social login, the appropriate user creation class is picked by the factory, it creates the user, and then logs them in.
After wiring this up, we test it end-to-end with Twitter, making sure it works, and then set the stage for adding other providers (like GitHub). Thanks to the new factory and contract, this will now be super straightforward. In the next episode, we'll see just how easy adding another provider is now that our structure is solid.