Skip to content

Global middleware option #114

@felix-dolderer

Description

@felix-dolderer

Desired Behaviour

It's be very nice to have an option to use the loggedIn middleware as a global middleware.

Current Situation

Currently the middleware needs to be added to each page individually or you need to create your own global middleware.

Creating your own global middleware either means copy / pasting the code from the module or importing directly from node_modules, because the middleware is not exposed so it could be directly imported.

Possible Implementations

I see two options to achieve the desired outcome:

  1. Add a configuration option that adds a global middleware
  2. Expose the middleware functions so they can be imported in a simple and typesafe way

Current workaround

My current implementation / workaround looks like this:

/*
 * middleware/01-loggedIn.global.ts
 */

import hankoLoggedIn from "../node_modules/@nuxtjs/hanko/dist/runtime/middleware/logged-in";

export default defineNuxtRouteMiddleware(async (to, from) => {
  // Don't trigger on same page navigation (changes to query or hash)
  if (process.server || to.path !== from.path) {
    const loggedInCheck = await hankoLoggedIn(to, from);
    if (loggedInCheck) return loggedInCheck;
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions