Middleware

beditaAuth

The beditaAuth middleware deals with session user. It read user from session and put it in shared state usable via useBeditaAuth() composable.

It is also configurable to protect routes.

By default this middleware is added as global to middleware application stack in order to set user state in every page.
If you don't care about this behavior you can configure the middleware as not global.
nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    '@atlasconsulting/nuxt-bedita',
  ],
  bedita: {
    auth: {
      global: false,
    },
  },
})

Set authentication as required

You can configure authentication as required for the whole app

nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    '@atlasconsulting/nuxt-bedita',
  ],
  bedita: {
    auth: {
      required: true,
    },
  },
})

In this case every route of the app will be check for authenticated user. Unauthenticated users will be redirect to unauthenticatedRedirect property.

Public pages

It is possible to configure a set of routes for public pages i.e. pages accessible either from authenticated user than not authenticated.

This is mostly useful when the auth middleware is configure as global and required.

nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    '@atlasconsulting/nuxt-bedita',
  ],
  bedita: {
    auth: {
      global: true,
      required: true,
      publicRoutes: ['/credits', '/contacts'],
    },
  },
})

Routing roles guard

Sometimes you need to protect some routes so that only users with a specific role can access to those pages.

This can be done with roles guard:

nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    '@atlasconsulting/nuxt-bedita',
  ],
  bedita: {
    auth: {
      rolesGuard: {
        '/admin': ['admin'], // only users with admin role can access to routes that start with '/admin'
        '/manage/stuff': ['admin', 'manager'], // only users with 'admin' or 'manager' role can access
        '/profile': ['*'], // '*' means any role is authorized (all authenticated users can access to '/profile')
      },
    },
  },
})
When a user can't access to a route a 403 Forbidden error is thrown.

The redirect query string

When an anomyous user navigates to a protected route, the middleware redirects it to the configured unauthenticatedRedirect page appending a redirect query string containing the location where the user was trying to access.

In this way it is easy to redirect the user to that page at the end of login flow.

beditaRolesGuard

This middleware allows to protect pages to users with a specific role. This is an alternative way to Routing roles guard and is very useful when you prefer to have the allowed roles directly in page rather than in nuxt.config.ts configuration.

To use it you have to add middleware in the setup section of your page, for example:

<script setup lang="ts">
definePageMeta({
  middleware: 'beditaRolesGuard',
  beditaRolesGuard: {
    roles: ['admin'],
  },
});
</script>

In this way the page will be protected and accessible only to users that have admin role.