Cookie Authentication
Overview
Nuxt-Umbu provides first-class support for Laravel Sanctum SPA authentication using secure HTTP-only cookies and CSRF protection.
This is the recommended authentication strategy when your Nuxt and Laravel applications share the same top-level domain.
Official Laravel documentation: SPA Authentication
- Nuxt application →
domain.com - Laravel API →
api.domain.com
How Cookie Authentication Works
When using the Sanctum provider, Nuxt-Umbu automatically handles:
- CSRF cookie retrieval (
/sanctum/csrf-cookie) - Sending the
X-XSRF-TOKENheader - Managing HTTP-only session cookies
- Forwarding cookies and headers between CSR and SSR
- Authentication state handling
Authentication Flow
- User submits credentials to the configured
endpoints.login - Nuxt-Umbu ensures a CSRF cookie is retrieved
- Laravel authenticates the user and sets a session cookie
- All subsequent requests automatically include:
- Session cookie
X-XSRF-TOKENheader
- The promise resolves on success or rejects on error
- The developer must handle redirection manually
Example using .then() / .catch()
const { $auth } = useNuxtApp()
const form = ref({
email: '',
password: ''
})
const submit = () => {
$auth.loginWith('sanctum', form.value)
.then((response) => {
// authentication successful
navigateTo('/dashboard')
})
.catch((error) => {
// handle validation or authentication errors
console.error(error)
})
}
CSRF Handling
By default, Nuxt-Umbu follows Laravel Sanctum's standard CSRF naming conventions.
Currently, these values are fixed and not configurable:
| Option | Default Value |
|---|---|
csrf.cookie | XSRF-TOKEN |
csrf.header | X-XSRF-TOKEN |
The module automatically:
- Extracts the
XSRF-TOKENcookie from the server response - Sends its value in the
X-XSRF-TOKENheader on subsequent requests - Ensures CSRF protection works in both CSR and SSR environments
These defaults match Laravel Sanctum's expected behavior and require no additional configuration.
Note: Future versions of Nuxt-Umbu may allow customization of CSRF cookie and header names.
Laravel configuration
Your Laravel API should be configured properly to support Nuxt domain and share cookies:
- The Nuxt application domain should be registered in
statefuldomain list (SANCTUM_STATEFUL_DOMAINS) - The Nuxt application domain should be registered in
config/cors.phpinallowed_originsdomain list - Also
config/cors.phpconfiguration should havesupport_credentials=true - Sanctum
statefulApimiddleware should be enabled - The top-level domain should be used for the session (
SESSION_DOMAIN=.domain.com), orlocalhostduring development (without port)
If you notice incorrect behavior of the module or authentication flow, feel free to raise an issue!