-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathmodule.ts
More file actions
202 lines (189 loc) · 6.66 KB
/
module.ts
File metadata and controls
202 lines (189 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import type { InternalClerkScriptProps, Without } from '@clerk/shared/types';
import type { PluginOptions } from '@clerk/vue';
import {
addComponent,
addImports,
addImportsDir,
addPlugin,
addServerHandler,
addTypeTemplate,
createResolver,
defineNuxtModule,
updateRuntimeConfig,
} from '@nuxt/kit';
export type ModuleOptions = Without<PluginOptions, 'routerPush' | 'routerReplace' | 'publishableKey' | 'initialState'> &
InternalClerkScriptProps & {
publishableKey?: string;
/**
* Skip the automatic server middleware registration. When enabled, you'll need to
* register the middleware manually in your application.
*
* @default false
*
* @example
*
* ```ts
* // server/middleware/clerk.ts
* import { clerkMiddleware } from '@clerk/nuxt/server'
*
* export default clerkMiddleware((event) => {
* console.log('auth', event.context.auth())
* })
* ```
*/
skipServerMiddleware?: boolean;
};
export default defineNuxtModule<ModuleOptions>({
meta: {
name: PACKAGE_NAME,
version: PACKAGE_VERSION,
configKey: 'clerk',
compatibility: {
nuxt: '>=3.0.0',
},
},
setup(options, nuxt) {
// These values must be set (even as undefined) to allow runtime config to work properly.
// In Nuxt, having these keys defined (even as undefined) allows them to be overridden
// by environment variables following the pattern NUXT_PUBLIC_CLERK_* (e.g. NUXT_PUBLIC_CLERK_PUBLISHABLE_KEY).
// More info https://nuxt.com/docs/guide/going-further/runtime-config
void updateRuntimeConfig({
// Public keys exposed to client and shared with the server
public: {
clerk: {
...options,
publishableKey: options.publishableKey,
signInUrl: options.signInUrl,
signInFallbackRedirectUrl: options.signInFallbackRedirectUrl,
signUpFallbackRedirectUrl: options.signUpFallbackRedirectUrl,
signInForceRedirectUrl: options.signInForceRedirectUrl,
signUpForceRedirectUrl: options.signUpForceRedirectUrl,
signUpUrl: options.signUpUrl,
domain: options.domain,
// Using jsUrl/uiUrl instead of __internal_clerkJSUrl/__internal_clerkUIUrl to support
// NUXT_PUBLIC_CLERK_JS_URL and NUXT_PUBLIC_CLERK_UI_URL env vars.
jsUrl: options.__internal_clerkJSUrl,
uiUrl: options.__internal_clerkUIUrl,
clerkJSVersion: options.__internal_clerkJSVersion,
clerkUIVersion: options.__internal_clerkUIVersion,
// prefetchUI config: can be false or undefined
prefetchUI: options.prefetchUI,
isSatellite: options.isSatellite,
// Listed explicitly so it can be overridden via NUXT_PUBLIC_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING.
unsafe_disableDevelopmentModeConsoleWarning: options.unsafe_disableDevelopmentModeConsoleWarning,
// Backend specific variables that are safe to share.
// We want them to be overridable like the other public keys (e.g NUXT_PUBLIC_CLERK_PROXY_URL)
proxyUrl: options.proxyUrl,
// Deprecated: use NUXT_CLERK_API_URL and NUXT_CLERK_API_VERSION instead.
// Kept for backwards compatibility with NUXT_PUBLIC_CLERK_API_URL / NUXT_PUBLIC_CLERK_API_VERSION.
apiUrl: undefined,
apiVersion: undefined,
},
},
// Private keys available only on within server-side
clerk: {
secretKey: undefined,
machineSecretKey: undefined,
jwtKey: undefined,
webhookSigningSecret: undefined,
apiUrl: undefined,
apiVersion: undefined,
},
});
const resolver = createResolver(import.meta.url);
// Handle Nuxt-specific imports (e.g #imports)
nuxt.options.build.transpile.push(resolver.resolve('./runtime'));
// Optimize @clerk/vue to avoid missing injection Symbol key errors
nuxt.options.vite.optimizeDeps = nuxt.options.vite.optimizeDeps || {};
nuxt.options.vite.optimizeDeps.include = nuxt.options.vite.optimizeDeps.include || [];
nuxt.options.vite.optimizeDeps.include.push('@clerk/vue');
// Add the `@clerk/vue` plugin
addPlugin(resolver.resolve('./runtime/plugin'));
// Allow skipping installing the server middleware
// and let the user handle it manually
if (!options.skipServerMiddleware) {
addServerHandler({
middleware: true,
handler: resolver.resolve('./runtime/server/middleware'),
});
}
// Adds TS support for `event.context.auth()` in event handlers
addTypeTemplate(
{
filename: 'types/clerk.d.ts',
getContents: () => `import type { AuthFn } from '@clerk/nuxt/server';
declare module 'h3' {
interface H3EventContext {
auth: AuthFn;
}
}
`,
},
{ nitro: true },
);
// Add auto-imports for Clerk components, composables and client utils
addImportsDir(resolver.resolve('./runtime/composables'));
addImports([
{
name: 'createRouteMatcher',
from: resolver.resolve('./runtime/client'),
},
{
name: 'updateClerkOptions',
from: resolver.resolve('./runtime/client'),
},
]);
// Components that use path-based routing (wrapped components)
const wrappedComponents = [
'SignIn',
'SignUp',
'UserProfile',
'OrganizationProfile',
'CreateOrganization',
'OrganizationList',
] as const;
wrappedComponents.forEach(component => {
void addComponent({
name: component,
export: component,
filePath: resolver.resolve('./runtime/components'),
});
});
// Other components exported directly from @clerk/vue
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const otherComponents: Array<keyof typeof import('@clerk/vue')> = [
// Authentication Components
'GoogleOneTap',
// Unstyled Components
'SignInButton',
'SignOutButton',
'SignUpButton',
'SignInWithMetamaskButton',
// User Components
'UserButton',
// Organization Components
'OrganizationSwitcher',
// Billing Components
'PricingTable',
// Control Components
'ClerkLoaded',
'ClerkLoading',
'RedirectToSignIn',
'RedirectToSignUp',
'RedirectToUserProfile',
'RedirectToOrganizationProfile',
'RedirectToCreateOrganization',
'Show',
'Waitlist',
// API Keys
'APIKeys',
];
otherComponents.forEach(component => {
void addComponent({
name: component,
export: component,
filePath: '@clerk/vue',
});
});
},
});