Custom API (Laravel) with Nuxt

I'm starting a nuxt app and I'm having trouble implementing session management with a custom backend API (in this case its Laravel). Any guides out there? All guides I've seen implements server code using nitro engine but I already have a backend for that. Is there a simpler way to do what I'm aiming for? Thank you!
2 Replies
kapa.ai
kapa.ai2w ago
Hi @Q Coldwater. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- If you already have a custom backend API (Laravel) and want to implement session management in your Nuxt app, you do not need to use Nuxt's server directory or Nitro for your API endpoints. Instead, you can interact directly with your Laravel backend using Nuxt's data fetching composables like useFetch or $fetch. Here are some key points from the knowledge sources: - You only need to use the server/api directory in Nuxt if you want to create endpoints directly tied to your Nuxt app. If your backend is already handled by Laravel, you can simply call those endpoints from your Nuxt frontend. The two approaches (Nuxt server/api vs. external backend like Laravel) are separate and not required to be used together unless you have a specific use case for both (e.g., proxying, custom logic, etc.) Which to use: $fetch, useAsyncData or useFetch for GET and POST requests in Nuxt 3?, Is it required to use server folder in Nuxt 3 when we have API endpoints?. - For session management and authentication, you can use libraries like nuxt-auth-utils or nuxt-auth-sanctum, depending on your authentication strategy. If you use Laravel Sanctum, make sure your CSRF and cookie settings are correctly configured for cross-domain requests. Issues like "CSRF token mismatch" are common when frontend and backend are on different domains or protocols, and you need to ensure cookies are set with the correct domain, SameSite, and secure flags. Also, both frontend and backend should use HTTPS in production to avoid such issues Laraval Santcum 419 "CSRF token missmatch" error with nuxt. - You can use useFetch or $fetch in your Nuxt components to interact with your Laravel API. For authentication flows, you might POST to your Laravel login endpoint, then handle session cookies as needed. Error handling and session refresh can be managed in your Nuxt app as shown in the Nuxt authentication recipe, but instead of using a local API route, you point to your Laravel endpoints Sessions and Authentication. In summary:
You do not need to use Nitro/server/api if your backend is already handled by Laravel. Use Nuxt's data fetching composables to interact with your Laravel API, and ensure your authentication/session cookies are configured for cross-origin use if your frontend and backend are on different domains. For more details on handling authentication and sessions, see the Nuxt authentication recipe and adapt the API calls to your Laravel endpoints. If you need a specific example or run into issues with cookies or CSRF, let me know! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@374961301530738698> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?