Skip to content

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:

OptionDescriptionDefault
apiUrlThe base URL of the Laravel API.Required
authModeThe authentication mode.'cookie'
appOriginUrlThe current application URL for the Referrer header. (Optional)None
userStateKeyThe key to use to store the authenticated user in the useState variable.'sanctum.authenticated.user'
token.storageKeyThe key to store the token in storage.'AUTH_TOKEN'
token.providerThe storage provider to use for the token.'cookie'
fetchClientOptions.retryAttemptsNumber of retry attempts for failed requests.false
csrf.cookieNameName of the CSRF cookie.'XSRF-TOKEN'
csrf.headerNameName of the CSRF header.'X-XSRF-TOKEN'
sanctumEndpoints.csrfEndpoint to request a new CSRF token.'/sanctum/csrf-cookie'
sanctumEndpoints.loginEndpoint to authenticate the user.'/login'
sanctumEndpoints.logoutEndpoint to log out the user.'/logout'
sanctumEndpoints.userEndpoint to fetch current user data.'/api/user'
redirect.enableIntendedRedirectKeep the requested route after login.false
redirect.loginPathPath to redirect when access requires authentication.'/login'
redirect.guestOnlyRedirectURL to redirect to when guest access is required.'/'
redirect.redirectToAfterLoginURL to redirect to after a successful login.'/'
redirect.redirectToAfterLogoutURL to redirect to after logout.'/'
middlewareNames.authMiddleware name for authenticated users.'$auth'
middlewareNames.guestMiddleware name for guest users.'$guest'
logLevelLog 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.