Skip to content

Customizing Middleware Names ​

The default names for these middlewares are $auth and $guest, but you have the flexibility to rename them to suit your project's conventions or preferences. This can be done through the module's configuration settings.

How to Rename Middlewares ​

To rename the $auth and $guest middlewares, you can specify new names in the middlewareNames option within the laravelSanctum configuration in your nuxt.config.ts or nuxt.config.js file. Here's how you can do it:

javascript
export default defineNuxtConfig({
    modules: ['nuxt-sanctum-authentication'],

    laravelSanctum: {
        apiUrl: 'http://laravel-api.test',  // Your Laravel API base URL

        middlewareNames: {
            auth: '$auth',    // Custom name for the auth middleware
            guest: '$guest',  // Custom name for the guest middleware
        },
    },
});

In this example, you can replace '$auth' and '$guest' with any other names you prefer. Once renamed, you will need to use the new names in your page components or route configurations.

Example with Custom Middleware Names ​

javascript
export default defineNuxtConfig({
    modules: ['nuxt-sanctum-authentication'],

    laravelSanctum: {
        apiUrl: 'http://laravel-api.test',

        middlewareNames: {
            auth: 'authenticated',  // Renamed auth middleware
            guest: 'guestOnly',     // Renamed guest middleware
        },
    },
});

Now, to use these custom middleware names in your pages:

javascript
definePageMeta({
  middleware: ['authenticated'], // Applies the renamed auth middleware
});
javascript
definePageMeta({
  middleware: ['guestOnly'], // Applies the renamed guest middleware
});

By leveraging these middlewares, you can effectively manage user access to different parts of your application, ensuring that only the right users see the right content.