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' |
userResponseWrapperKey | This option lets you specify the key used to find user data in the response from the sanctumEndpoints.user API. | null |
token.storageKey | The key to store the token in storage. | 'AUTH_TOKEN' |
token.provider | The storage provider to use for the token. | 'cookie' |
token.responseKey | This option specifies the key used to retrieve the authentication token from the response of the sanctumEndpoints.login API. | 'token' |
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: ["@qirolab/nuxt-sanctum-authentication"],
laravelSanctum: {
/**
* The base URL of the Laravel API.
* @example http://laravel-api.test
* @required
*/
apiUrl: 'http://laravel-api.test',
/**
* Authentication mode: 'cookie' or 'token'
* - 'cookie' for SPA cookie authentication,
* - 'token' for API token authentication
*
* @default 'cookie'
*/
authMode: 'cookie',
/**
* The key to use to store the authenticated user in the `useState` variable.
* @default 'sanctum.authenticated.user'
*/
userStateKey: 'sanctum.authenticated.user',
/**
* This option lets you specify the key used to find user data in the response
* from the `sanctumEndpoints.user` API.
*
* For example:
* - If the API response looks like `{ data: { ... } }`, you should set
* `userResponseWrapperKey` to `'data'` to access the user information.
*
* - If the response is `{ data: { user: { ... } } }`, then use
* `userResponseWrapperKey` as `'data.user'` to get the user object.
*
* This makes it easy to work with different formats of API responses.
*
* @type {null | string}
* @default null
*/
userResponseWrapperKey: 'data';
/**
* The token specific options.
*/
token: {
/**
* The key to store the token in the storage.
* @default 'AUTH_TOKEN'
*/
storageKey: 'AUTH_TOKEN',
/**
* The storage provider to use for the token.
* Options: 'cookie' or 'localStorage'
* @default 'cookie'
*/
provider: 'cookie',
/**
* This option specifies the key used to retrieve the authentication token
* from the response of the `sanctumEndpoints.login` API.
*
* By default, this key is set to `'token'`. If your API response uses a
* different key for the token, you can change this option to match it.
*
* For example, if your API response looks like this:
* ```
* {
* "data": {
* "auth_token": "your_token_here"
* }
* }
* ```
* You would set `responseKey` to `'data.auth_token'` to access the token.
*
* @type {string}
* @default 'token'
*/
responseKey: 'token';
},
/**
* OFetch client specific options.
*/
fetchClientOptions: {
/**
* The number of times to retry a request when it fails.
* @default false
*/
retryAttempts: false,
},
/**
* CSRF token specific options.
*/
csrf: {
/**
* Name of the CSRF cookie to extract from server response.
* @default 'XSRF-TOKEN'
*/
cookieName: 'XSRF-TOKEN',
/**
* Name of the CSRF header to pass from client to server.
* @default 'X-XSRF-TOKEN'
*/
headerName: 'X-XSRF-TOKEN',
},
/**
* Laravel Sanctum API endpoints.
*/
sanctumEndpoints: {
/**
* The endpoint to request a new CSRF token.
* @default '/sanctum/csrf-cookie'
*/
csrf: '/sanctum/csrf-cookie',
/**
* The endpoint to send user credentials to authenticate.
* @default '/login'
*/
login: '/login',
/**
* The endpoint to destroy current user session.
* @default '/logout'
*/
logout: '/logout',
/**
* The endpoint to fetch current user data.
* @default '/api/user'
*/
user: '/api/user',
},
/**
* Behavior of the plugin redirects when user is authenticated or not.
*/
redirect: {
/**
* Determines whether to keep the requested route when redirecting after login.
* @default false
*/
enableIntendedRedirect: false,
/**
* Path to redirect to when access requires user authentication.
* If set to false, a 403 error is triggered.
* @default '/login'
*/
loginPath: '/login',
/**
* URL to redirect to when guest access is required (user must not be authenticated).
* If set to false, the plugin will throw an 403 error.
* @default '/'
*/
guestOnlyRedirect: '/',
/**
* URL to redirect to after a successful login.
* If set to false, no redirection occurs.
* @default '/'
*/
redirectToAfterLogin: '/',
/**
* URL to redirect to after logout.
* If set to false, no redirection occurs.
* @default '/'
*/
redirectToAfterLogout: '/',
},
middlewareNames: {
/**
* Middleware name for routes that require authentication.
* @default '$auth'
*/
auth: '$auth',
/**
* Middleware name for routes that require the user to be a guest.
* @default '$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.