In this episode, we dive into setting up and testing authentication from our client app to our API. Since we don't have any users to start with, we use a factory to generate a fake user in the database (and customize the details for ourselves). Then, we tidy up our client app—removing the default logo, about page, extra styles, and other boilerplate code so we've got a nice, clean slate to work with.
With the app cleaned up, we create a quick and simple login form, hooking up fields for email and password. To make network requests, we install and use Axios. We use Vue's reactive
method for our form data and bind it straight to the input fields, even pre-filling it to save typing while testing!
The first time we try sending a login request, we run into some issues—like getting a 404 error because we're posting to the wrong endpoint. We fix this by setting Axios defaults, particularly the baseURL
and configuring it for Laravel Sanctum (setting withCredentials
and CSRF token stuff). This gets us past the CORS and CSRF errors, but there's one more step: fetching the correct CSRF cookie before logging in.
After that, login finally works and we test authentication by making a request to the /api/user
endpoint. We see our user data come back, confirming everything's working as expected!
The episode wraps up by noting that while we've proven the authentication flow, our code is messy and jammed into a single component. Next, we'll look into separating this logic into a composable for cleaner, reusable code and better state management.