hanko icon indicating copy to clipboard operation
hanko copied to clipboard

Global middleware option

Open felix-dolderer opened this issue 1 year ago • 0 comments

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;
  }
});

felix-dolderer avatar Mar 27 '24 10:03 felix-dolderer