Skip to content

Commit

Permalink
register middleware on module level instead of plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
manchenkoff committed Sep 21, 2023
1 parent 6a25e07 commit 67c1f51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
addPlugin,
createResolver,
addImportsDir,
addRouteMiddleware,
} from '@nuxt/kit';
import { defu } from 'defu';
import { SanctumOptions } from './types';
Expand Down Expand Up @@ -54,6 +55,16 @@ export default defineNuxtModule<SanctumOptions>({
);

addImportsDir(resolver.resolve('./runtime/composables'));

addRouteMiddleware({
name: 'sanctum:auth',
path: resolver.resolve('./runtime/middleware/sanctum.auth'),
});
addRouteMiddleware({
name: 'sanctum:guest',
path: resolver.resolve('./runtime/middleware/sanctum.guest'),
});

addPlugin(resolver.resolve('./runtime/plugin'));
},
});
7 changes: 1 addition & 6 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { FetchError } from 'ofetch';
import { addRouteMiddleware, defineNuxtPlugin } from '#app';
import { defineNuxtPlugin } from '#app';
import { createHttpClient } from './httpFactory';
import { useSanctumUser } from './composables/useSanctumUser';
import sanctumAuth from './middleware/sanctum.auth';
import sanctumGuest from './middleware/sanctum.guest';
import { SanctumOptions } from '../types';

function handleIdentityLoadError(error: Error) {
Expand All @@ -24,9 +22,6 @@ export default defineNuxtPlugin(async (nuxtApp) => {

const options = nuxtApp.$config.public.sanctum as SanctumOptions;

addRouteMiddleware('sanctum:auth', sanctumAuth);
addRouteMiddleware('sanctum:guest', sanctumGuest);

if (process.server && user.value === null) {
try {
user.value = await client(options.endpoints.user);
Expand Down

0 comments on commit 67c1f51

Please sign in to comment.