In this episode, we're taking the registration logic out of our page components and moving it into its own composable to keep things organized and easier to maintain. First, we create a new composables
directory and set up a use-auth.ts
file, which is going to hold all of our authentication-related methods and logic that aren't already handled by the package we're using.
We start simple by defining a register
method in our new composable, hook it up to the registration page, and make sure everything is connected by logging to the console. Once that's working, we move the real registration logic over, making use of our existing sanctumFetch
function, and pass the registration form data to it.
Along the way, we debug a validation error (that 422 status!) and show how you can tweak the API validation rules if needed. After getting registration working, we make sure that when a user registers, their authenticated state is updated right away. For that, we bring in a function to refresh the user identity and call it after a successful registration.
Finally, we add a redirect to the dashboard after registration, do some cleanup, and check for any lingering sessions that might mess with our workflow. We end with everything buttoned up: registration is handled in a reusable composable, the user gets signed in and redirected, and our codebase is a lot tidier!