Skip to content

Commit

Permalink
feat: simplified module config
Browse files Browse the repository at this point in the history
  • Loading branch information
manchenkoff committed Aug 30, 2024
1 parent e784f88 commit b3bad10
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 243 deletions.
65 changes: 33 additions & 32 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import type { SanctumModuleOptions } from './runtime/types/options';
import type { ModuleOptions } from './runtime/types/options'

export const defaultModuleOptions: Partial<SanctumModuleOptions> = {
mode: 'cookie',
userStateKey: 'sanctum.user.identity',
redirectIfAuthenticated: false,
redirectIfUnauthenticated: false,
endpoints: {
csrf: '/sanctum/csrf-cookie',
login: '/login',
logout: '/logout',
user: '/api/user',
},
csrf: {
cookie: 'XSRF-TOKEN',
header: 'X-XSRF-TOKEN',
},
client: {
retry: false,
},
redirect: {
keepRequestedRoute: false,
onLogin: '/',
onLogout: '/',
onAuthOnly: '/login',
onGuestOnly: '/',
},
globalMiddleware: {
enabled: false,
allow404WithoutAuth: true,
},
logLevel: 3,
};
export const defaultModuleOptions: ModuleOptions = {
baseUrl: 'http://localhost:80',
mode: 'cookie',
userStateKey: 'sanctum.user.identity',
redirectIfAuthenticated: false,
redirectIfUnauthenticated: false,
endpoints: {
csrf: '/sanctum/csrf-cookie',
login: '/login',
logout: '/logout',
user: '/api/user',
},
csrf: {
cookie: 'XSRF-TOKEN',
header: 'X-XSRF-TOKEN',
},
client: {
retry: false,
},
redirect: {
keepRequestedRoute: false,
onLogin: '/',
onLogout: '/',
onAuthOnly: '/login',
onGuestOnly: '/',
},
globalMiddleware: {
enabled: false,
allow404WithoutAuth: true,
},
logLevel: 3,
}
133 changes: 58 additions & 75 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,70 @@
import {
defineNuxtModule,
addPlugin,
createResolver,
addImportsDir,
addRouteMiddleware,
useLogger,
} from '@nuxt/kit';
import { defu } from 'defu';
import { defaultModuleOptions } from './config';
import type { SanctumGlobalMiddlewarePageMeta } from './runtime/types/meta';
import type { SanctumModuleOptions } from './runtime/types/options';
import { registerTypeTemplates } from './templates';

type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

declare module '#app' {
interface PageMeta {
/**
* Sanctum global middleware page configuration.
*/
sanctum?: Partial<SanctumGlobalMiddlewarePageMeta>;
}
}

const MODULE_NAME = 'nuxt-auth-sanctum';

export type ModuleOptions = DeepPartial<SanctumModuleOptions>;
export type ModulePublicRuntimeConfig = { sanctum: ModuleOptions };
defineNuxtModule,
addPlugin,
createResolver,
addImportsDir,
addRouteMiddleware,
useLogger,
} from '@nuxt/kit'
import { defu } from 'defu'
import { defaultModuleOptions } from './config'
import type { ModuleOptions } from './runtime/types/options'
import { registerTypeTemplates } from './templates'

const MODULE_NAME = 'nuxt-auth-sanctum'

export type ModulePublicRuntimeConfig = { sanctum: ModuleOptions }

export default defineNuxtModule<ModuleOptions>({
meta: {
name: MODULE_NAME,
configKey: 'sanctum',
compatibility: {
nuxt: '>=3.12.0',
},
meta: {
name: MODULE_NAME,
configKey: 'sanctum',
compatibility: {
nuxt: '>=3.12.0',
},
},

defaults: defaultModuleOptions,

setup(options, nuxt) {
const resolver = createResolver(import.meta.url);
defaults: defaultModuleOptions,

const runtimeDir = resolver.resolve('./runtime');
nuxt.options.build.transpile.push(runtimeDir);
setup(_options, _nuxt) {
const resolver = createResolver(import.meta.url)
const sanctumConfig = defu(
_nuxt.options.runtimeConfig.public.sanctum,
_options,
)

const sanctumConfig = defu(
nuxt.options.runtimeConfig.public.sanctum as any,
options
);
_nuxt.options.build.transpile.push(resolver.resolve('./runtime'))
_nuxt.options.runtimeConfig.public.sanctum = sanctumConfig

nuxt.options.runtimeConfig.public.sanctum = sanctumConfig;
const logger = useLogger(MODULE_NAME, {
level: sanctumConfig.logLevel,
})

const logger = useLogger(MODULE_NAME, {
level: sanctumConfig.logLevel,
});
addPlugin(resolver.resolve('./runtime/plugin'))
addImportsDir(resolver.resolve('./runtime/composables'))

addPlugin(resolver.resolve('./runtime/plugin'));
addImportsDir(resolver.resolve('./runtime/composables'));
if (sanctumConfig.globalMiddleware.enabled) {
addRouteMiddleware({
name: 'sanctum:auth:global',
path: resolver.resolve('./runtime/middleware/sanctum.global'),
global: true,
})

if (sanctumConfig.globalMiddleware.enabled) {
addRouteMiddleware({
name: 'sanctum:auth:global',
path: resolver.resolve('./runtime/middleware/sanctum.global'),
global: true,
});

logger.info('Sanctum module initialized with global middleware');
} else {
addRouteMiddleware({
name: 'sanctum:auth',
path: resolver.resolve('./runtime/middleware/sanctum.auth'),
});
addRouteMiddleware({
name: 'sanctum:guest',
path: resolver.resolve('./runtime/middleware/sanctum.guest'),
});

logger.info('Sanctum module initialized w/o global middleware');
}
logger.info('Sanctum module initialized with global middleware')
}
else {
addRouteMiddleware({
name: 'sanctum:auth',
path: resolver.resolve('./runtime/middleware/sanctum.auth'),
})
addRouteMiddleware({
name: 'sanctum:guest',
path: resolver.resolve('./runtime/middleware/sanctum.guest'),
})

logger.info('Sanctum module initialized w/o global middleware')
}

registerTypeTemplates(resolver);
},
});
registerTypeTemplates(resolver)
},
})
Loading

0 comments on commit b3bad10

Please sign in to comment.