Configuration
Required Configuration
The only required configuration option is apiUrl
:
javascript
laravelSanctum: {
apiUrl: 'http://laravel-api.test',
}
Available Options
Here are the available configuration options:
Option | Description | Default |
---|---|---|
apiUrl | The base URL of the Laravel API. | Required |
authMode | The authentication mode. | 'cookie' |
appOriginUrl | The current application URL for the Referrer header. (Optional) | None |
userStateKey | The key to use to store the authenticated user in the useState variable. | 'sanctum.authenticated.user' |
token.storageKey | The key to store the token in storage. | 'AUTH_TOKEN' |
token.provider | The storage provider to use for the token. | 'cookie' |
fetchClientOptions.retryAttempts | Number of retry attempts for failed requests. | false |
csrf.cookieName | Name of the CSRF cookie. | 'XSRF-TOKEN' |
csrf.headerName | Name of the CSRF header. | 'X-XSRF-TOKEN' |
sanctumEndpoints.csrf | Endpoint to request a new CSRF token. | '/sanctum/csrf-cookie' |
sanctumEndpoints.login | Endpoint to authenticate the user. | '/login' |
sanctumEndpoints.logout | Endpoint to log out the user. | '/logout' |
sanctumEndpoints.user | Endpoint to fetch current user data. | '/api/user' |
redirect.enableIntendedRedirect | Keep the requested route after login. | false |
redirect.loginPath | Path to redirect when access requires authentication. | '/login' |
redirect.guestOnlyRedirect | URL to redirect to when guest access is required. | '/' |
redirect.redirectToAfterLogin | URL to redirect to after a successful login. | '/' |
redirect.redirectToAfterLogout | URL to redirect to after logout. | '/' |
middlewareNames.auth | Middleware name for authenticated users. | '$auth' |
middlewareNames.guest | Middleware name for guest users. | '$guest' |
logLevel | Log level for the logger. | 3 |
Overriding Configuration
You can easily override any of the above configuration options in your nuxt.config.ts
file to suit your application's needs:
javascript
export default defineNuxtConfig({
// List of Nuxt modules to be included
modules: ['nuxt-sanctum-authentication'],
laravelSanctum: {
// The base URL of your Laravel API
apiUrl: 'http://laravel-api.test',
// Authentication mode: 'cookie' for SPA cookie authentication, 'token' for API token authentication
authMode: 'cookie',
// The key used to store the authenticated user in the `useState` variable
userStateKey: 'sanctum.authenticated.user',
token: {
// The key to store the token in the browser's storage
storageKey: 'AUTH_TOKEN',
// The storage provider to use for the token: 'cookie' or 'localStorage'
provider: 'cookie',
},
fetchClientOptions: {
// Number of retry attempts for failed HTTP requests, set to false to disable retries
retryAttempts: false,
},
csrf: {
// Name of the CSRF cookie used to protect against cross-site request forgery
cookieName: 'XSRF-TOKEN',
// Name of the CSRF header sent with requests
headerName: 'X-XSRF-TOKEN',
},
sanctumEndpoints: {
// Endpoint to request a new CSRF token from the server
csrf: '/sanctum/csrf-cookie',
// Endpoint used for user authentication
login: '/login',
// Endpoint used to log out users
logout: '/logout',
// Endpoint to retrieve the currently authenticated user's data
user: '/api/user',
},
redirect: {
// Preserve the originally requested route, redirecting users there after login
enableIntendedRedirect: false,
// Path to redirect users when a page requires authentication
loginPath: '/login',
// URL to redirect users to when guest-only access is required
guestOnlyRedirect: '/',
// URL to redirect to after a successful login
redirectToAfterLogin: '/',
// URL to redirect to after logging out
redirectToAfterLogout: '/',
},
middlewareNames: {
// Middleware name for routes that require authentication
auth: '$auth',
// Middleware name for routes that require the user to be a guest
guest: '$guest',
},
// Sets the logging level: 1 for errors only, 3 for all logs
logLevel: 3,
},
});
This configuration file provides a comprehensive setup for the nuxt-sanctum-authentication
module, allowing you to customize how authentication is handled and how the module interacts with your application.