forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.ts
More file actions
142 lines (133 loc) · 4.54 KB
/
module.ts
File metadata and controls
142 lines (133 loc) · 4.54 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
import type { LoadClerkJsScriptOptions } from '@clerk/shared/loadClerkJsScript';
import {
addComponent,
addImportsDir,
addPlugin,
addServerHandler,
createResolver,
defineNuxtModule,
updateRuntimeConfig,
} from '@nuxt/kit';
export type ModuleOptions = Omit<LoadClerkJsScriptOptions, 'routerPush' | 'routerReplace'> & {
/**
* 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,
clerkJSUrl: options.clerkJSUrl,
clerkJSVariant: options.clerkJSVariant,
clerkJSVersion: options.clerkJSVersion,
// 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,
apiUrl: 'https://api.clerk.com',
apiVersion: 'v1',
},
},
// Private keys available only on within server-side
clerk: {
secretKey: undefined,
jwtKey: 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'),
});
}
// Add auto-imports for Clerk components, composables and client utils
addImportsDir(resolver.resolve('./runtime/composables'));
addImportsDir(resolver.resolve('./runtime/client'));
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const components: Array<keyof typeof import('@clerk/vue')> = [
// Authentication Components
'SignIn',
'SignUp',
// Unstyled Components
'SignInButton',
'SignOutButton',
'SignUpButton',
'SignInWithMetamaskButton',
// User Components
'UserButton',
'UserProfile',
// Organization Components
'CreateOrganization',
'OrganizationProfile',
'OrganizationSwitcher',
'OrganizationList',
// Control Components
'ClerkLoaded',
'ClerkLoading',
'Protect',
'RedirectToSignIn',
'RedirectToSignUp',
'RedirectToUserProfile',
'RedirectToOrganizationProfile',
'RedirectToCreateOrganization',
'SignedIn',
'SignedOut',
'Waitlist',
];
components.forEach(component => {
void addComponent({
name: component,
export: component,
filePath: '@clerk/vue',
});
});
},
});