Server utils

Middleware

Nuxt middleware integration

Middleware

This module can automatically add a global middleware to your Nuxt server. You can enable it by setting globalMiddlewareEnabled under the middleware section of the config. The middleware automatically redirects all requests to /auth/login if the user is not logged in and forwards the originally requested URL as callbackRedirectUrl. You can disable this behavior by setting redirect to false in the middleware configuration. After a successful callback, the user is redirected back to this original URL unless providers.<provider>.callbackRedirectUrl is explicitly configured. In that case, the configured callback redirect takes precedence. The /auth/login route is only configured if you have defined a default provider. If you want to use a custom login page and keep your default provider or don't want to set a default provider at all, you can set customLoginPage to true in the middleware configuration.

If you set customLoginPage to true, you have to manually add a login page to your Nuxt app under /auth/login. You can use the login method from the useOidcAuth composable to redirect the user to the respective provider login page. Setting customLogoutPage to true will disable the /auth/logout route. This route, if a default provider is set, redirects to the default providers logout page (/auth/<provider>/logout). You have to manually add a logout page to your Nuxt app under /auth/logout and use the logout method from the useOidcAuth composable to logout the user or make sure that you always provide the optional provider parameter to the logout method.

<script setup>
const { logout, currentProvider } = useOidcAuth()
</script>

<template>
  <button @click="logout(currentProvider)">
    Logout
  </button>
</template>
Everything under the /auth path is not protected by the global middleware. Make sure to not use this path for any other purpose than authentication.