-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e784f88
commit b3bad10
Showing
3 changed files
with
230 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
}) |
Oops, something went wrong.