In this episode, we're tackling user registration since the sanctum-auth package we pulled in doesn't include out-of-the-box functionality for creating accounts. We'll build out a basic register page ourselves and, along the way, dive a little into what's happening under the hood when the package hits our backend.
We'll start by copying the login page over to create a new register view, updating it to include a name field (since we need name, email, and password for registration). After wiring up the form and the TypeScript interfaces, we'll add a "Create an account" link to our navigation for users who aren't logged in.
Next, we'll try setting up a traditional fetch request to register a new user, but we'll immediately run into issues — like CSRF (Cross-Site Request Forgery) token mismatches. We'll take a quick look at the Laravel documentation to understand why that's happening, then demo the correct approach: using another composable from the sanctum-auth package called useSanctumClient
. This function works like useFetch
, but handles the CSRF token for us automatically, making sure our requests go through without a hitch.
Lastly, we see that registration now works (except for duplicate users) and talk about when you need to use this special fetch method (for Fortify routes with CSRF protection) versus standard API routes. We finish the episode by tidying up and planning to organize our custom requests into their own composable for a cleaner and more maintainable codebase.
By the end of this video, you'll have a working registration form, understand CSRF token flow with Laravel Sanctum, and know how to handle protected endpoints properly in your Nuxt app.