In this episode, we focus on adding the QR code display to our 2FA (Two-Factor Authentication) modal. We start by cleaning up the UI text and layout to properly guide users through setting up two-factor authentication. Then, we add a placeholder for the QR code and discuss how we'll eventually need a form for entering the code from their authenticator app.
Next, we talk through the process of actually fetching the QR code image from the backend. Since the endpoint gives us back just an SVG rather than a full Inertia response, we choose to use Axios for this request, which is totally fine for situations like this. We console log out the response to make sure we're getting the data we expect, and then wire up the component to store the SVG's content in a ref.
With the SVG properly stored, we use Vue's v-html
directive to insert the SVG into the DOM safely (since it's coming directly from Laravel, we can trust it). We also add a bit of styling to make the QR code look decent within our modal.
Along the way, we run into an issue: if the user's password hasn't been recently confirmed, the backend wants to verify that before giving us the QR code. We handle this by catching the error and triggering the password confirmation modal as needed, checking for the right HTTP status, and rerunning the flow afterward.
By the end, we have our QR code showing up nicely and ready for the next step: scanning it with an authenticator app and building out the code entry form. In the next episode, we'll handle that part and make sure the user's two-factor authentication is fully set up!