cobras-auth-nuxt 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +302 -0
- package/dist/module.cjs +5 -0
- package/dist/module.d.mts +134 -0
- package/dist/module.d.ts +134 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +112 -0
- package/dist/runtime/components/CobrasDevTools.vue +89 -0
- package/dist/runtime/composables/useCobrasAuth.d.ts +0 -0
- package/dist/runtime/composables/useCobrasAuth.js +99 -0
- package/dist/runtime/composables/useCobrasDevTools.d.ts +0 -0
- package/dist/runtime/composables/useCobrasDevTools.js +69 -0
- package/dist/runtime/composables/useCobrasMode.d.ts +0 -0
- package/dist/runtime/composables/useCobrasMode.js +25 -0
- package/dist/runtime/middleware/auth.d.ts +0 -0
- package/dist/runtime/middleware/auth.js +36 -0
- package/dist/runtime/middleware/internal.d.ts +0 -0
- package/dist/runtime/middleware/internal.js +19 -0
- package/dist/runtime/plugins/auth.client.d.ts +0 -0
- package/dist/runtime/plugins/auth.client.js +122 -0
- package/dist/runtime/plugins/auth.server.d.ts +0 -0
- package/dist/runtime/plugins/auth.server.js +69 -0
- package/dist/runtime/server/api/exchange.post.d.ts +0 -0
- package/dist/runtime/server/api/exchange.post.js +49 -0
- package/dist/runtime/server/api/logout.post.d.ts +0 -0
- package/dist/runtime/server/api/logout.post.js +27 -0
- package/dist/runtime/server/api/refresh.post.d.ts +0 -0
- package/dist/runtime/server/api/refresh.post.js +24 -0
- package/dist/runtime/server/api/verify.get.d.ts +0 -0
- package/dist/runtime/server/api/verify.get.js +50 -0
- package/dist/types.d.mts +7 -0
- package/dist/types.d.ts +7 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# @cobras/auth-nuxt
|
|
2
|
+
|
|
3
|
+
Nuxt 3/4 module for integrating with the Cobras Auth service. Supports two modes:
|
|
4
|
+
|
|
5
|
+
- **Internal Mode**: Full SSO authentication for internal tools
|
|
6
|
+
- **Public Mode**: Public-facing sites with optional auth for dev tools/special features
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @cobras/auth-nuxt
|
|
12
|
+
# or
|
|
13
|
+
pnpm add @cobras/auth-nuxt
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Setup
|
|
17
|
+
|
|
18
|
+
Add to your `nuxt.config.ts`:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
export default defineNuxtConfig({
|
|
22
|
+
modules: ['@cobras/auth-nuxt'],
|
|
23
|
+
|
|
24
|
+
cobrasAuth: {
|
|
25
|
+
// Required: URL of your Cobras Auth service
|
|
26
|
+
authServiceUrl: 'https://cobras-auth-app-production.up.railway.app',
|
|
27
|
+
|
|
28
|
+
// 'internal' = SSO for internal tools (blocks until auth checked)
|
|
29
|
+
// 'public' = public site with optional auth (non-blocking)
|
|
30
|
+
mode: 'public',
|
|
31
|
+
|
|
32
|
+
// Optional: Register your site in cobras-auth admin
|
|
33
|
+
siteId: 'your-site-id',
|
|
34
|
+
// or
|
|
35
|
+
siteDomain: 'your-site.com',
|
|
36
|
+
|
|
37
|
+
// Enable global auth middleware (internal mode typically)
|
|
38
|
+
globalMiddleware: false,
|
|
39
|
+
|
|
40
|
+
// Routes that don't require auth (internal mode)
|
|
41
|
+
publicRoutes: ['/', '/about', '/public/*'],
|
|
42
|
+
|
|
43
|
+
// Enable dev tools for authenticated users
|
|
44
|
+
enableDevTools: true,
|
|
45
|
+
devToolsKey: 'ctrl+shift+d',
|
|
46
|
+
|
|
47
|
+
// Debug logging
|
|
48
|
+
debug: false,
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Modes
|
|
54
|
+
|
|
55
|
+
### Public Mode (default)
|
|
56
|
+
|
|
57
|
+
For public-facing websites. Auth is checked on the client-side without blocking SSR.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// nuxt.config.ts
|
|
61
|
+
cobrasAuth: {
|
|
62
|
+
mode: 'public',
|
|
63
|
+
enableDevTools: true, // Show dev panel for logged-in team members
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Use cases:
|
|
68
|
+
- Marketing sites where team members can toggle feature flags
|
|
69
|
+
- Public apps where authenticated users get extra features
|
|
70
|
+
- Sites where you want to show dev tools to internal users
|
|
71
|
+
|
|
72
|
+
### Internal Mode
|
|
73
|
+
|
|
74
|
+
For internal tools requiring SSO. Auth is checked on the server before rendering.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// nuxt.config.ts
|
|
78
|
+
cobrasAuth: {
|
|
79
|
+
mode: 'internal',
|
|
80
|
+
globalMiddleware: true,
|
|
81
|
+
publicRoutes: ['/'], // Landing page is public
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Use cases:
|
|
86
|
+
- Admin dashboards
|
|
87
|
+
- Internal tools
|
|
88
|
+
- Apps that require authentication for all routes
|
|
89
|
+
|
|
90
|
+
## Usage
|
|
91
|
+
|
|
92
|
+
### Composables
|
|
93
|
+
|
|
94
|
+
#### `useCobrasAuth()`
|
|
95
|
+
|
|
96
|
+
Main composable for auth state and actions.
|
|
97
|
+
|
|
98
|
+
```vue
|
|
99
|
+
<script setup>
|
|
100
|
+
const {
|
|
101
|
+
user, // Ref<CobrasUser | null>
|
|
102
|
+
isAuthenticated, // ComputedRef<boolean>
|
|
103
|
+
isInternalUser, // ComputedRef<boolean> - alias for isAuthenticated
|
|
104
|
+
isAdmin, // ComputedRef<boolean>
|
|
105
|
+
mode, // 'internal' | 'public'
|
|
106
|
+
checkAuth, // () => Promise<void>
|
|
107
|
+
login, // (redirect?: string) => void
|
|
108
|
+
logout, // () => Promise<void>
|
|
109
|
+
} = useCobrasAuth()
|
|
110
|
+
</script>
|
|
111
|
+
|
|
112
|
+
<template>
|
|
113
|
+
<div v-if="isAuthenticated">
|
|
114
|
+
Welcome, {{ user?.name }}!
|
|
115
|
+
<button @click="logout">Logout</button>
|
|
116
|
+
</div>
|
|
117
|
+
<div v-else>
|
|
118
|
+
<button @click="login()">Login</button>
|
|
119
|
+
</div>
|
|
120
|
+
</template>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### `useCobrasMode()`
|
|
124
|
+
|
|
125
|
+
Check current mode and feature visibility.
|
|
126
|
+
|
|
127
|
+
```vue
|
|
128
|
+
<script setup>
|
|
129
|
+
const {
|
|
130
|
+
mode, // 'internal' | 'public'
|
|
131
|
+
isInternalMode, // boolean
|
|
132
|
+
isPublicMode, // boolean
|
|
133
|
+
showInternalFeatures, // boolean - true if user is authenticated
|
|
134
|
+
showAdminFeatures, // boolean - true if user is admin
|
|
135
|
+
devToolsEnabled, // boolean
|
|
136
|
+
} = useCobrasMode()
|
|
137
|
+
</script>
|
|
138
|
+
|
|
139
|
+
<template>
|
|
140
|
+
<AdminPanel v-if="showAdminFeatures" />
|
|
141
|
+
<InternalTools v-if="showInternalFeatures" />
|
|
142
|
+
</template>
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### `useCobrasDevTools()`
|
|
146
|
+
|
|
147
|
+
Control dev tools panel and feature flags.
|
|
148
|
+
|
|
149
|
+
```vue
|
|
150
|
+
<script setup>
|
|
151
|
+
const {
|
|
152
|
+
state, // Ref<DevToolsState>
|
|
153
|
+
isAvailable, // ComputedRef<boolean>
|
|
154
|
+
toggle, // () => void
|
|
155
|
+
open, // () => void
|
|
156
|
+
close, // () => void
|
|
157
|
+
setFeatureFlag, // (key: string, value: boolean) => void
|
|
158
|
+
getFeatureFlag, // (key: string) => boolean
|
|
159
|
+
toggleDebugMode, // () => void
|
|
160
|
+
} = useCobrasDevTools()
|
|
161
|
+
|
|
162
|
+
// Check a feature flag anywhere in your app
|
|
163
|
+
const showNewFeature = computed(() => getFeatureFlag('new-checkout-flow'))
|
|
164
|
+
</script>
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Global `$cobrasAuth`
|
|
168
|
+
|
|
169
|
+
Available via `useNuxtApp()` or in templates:
|
|
170
|
+
|
|
171
|
+
```vue
|
|
172
|
+
<template>
|
|
173
|
+
<span v-if="$cobrasAuth.isAuthenticated">
|
|
174
|
+
{{ $cobrasAuth.user?.name }}
|
|
175
|
+
</span>
|
|
176
|
+
</template>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Middleware
|
|
180
|
+
|
|
181
|
+
#### Global Middleware
|
|
182
|
+
|
|
183
|
+
Enable in config to protect all routes:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
cobrasAuth: {
|
|
187
|
+
globalMiddleware: true,
|
|
188
|
+
publicRoutes: ['/', '/login', '/public/*'],
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
#### Per-Route Protection
|
|
193
|
+
|
|
194
|
+
Use `cobras-internal` middleware on specific pages:
|
|
195
|
+
|
|
196
|
+
```vue
|
|
197
|
+
<!-- pages/admin.vue -->
|
|
198
|
+
<script setup>
|
|
199
|
+
definePageMeta({
|
|
200
|
+
middleware: 'cobras-internal'
|
|
201
|
+
})
|
|
202
|
+
</script>
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Dev Tools Component
|
|
206
|
+
|
|
207
|
+
Add the dev tools panel to your app (only shows for authenticated users):
|
|
208
|
+
|
|
209
|
+
```vue
|
|
210
|
+
<!-- app.vue or layouts/default.vue -->
|
|
211
|
+
<template>
|
|
212
|
+
<div>
|
|
213
|
+
<NuxtPage />
|
|
214
|
+
<CobrasDevTools />
|
|
215
|
+
</div>
|
|
216
|
+
</template>
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Toggle with keyboard shortcut (default: `Ctrl+Shift+D`).
|
|
220
|
+
|
|
221
|
+
## API Reference
|
|
222
|
+
|
|
223
|
+
### Module Options
|
|
224
|
+
|
|
225
|
+
| Option | Type | Default | Description |
|
|
226
|
+
|--------|------|---------|-------------|
|
|
227
|
+
| `authServiceUrl` | `string` | Railway URL | Cobras Auth service URL |
|
|
228
|
+
| `mode` | `'internal' \| 'public'` | `'public'` | Authentication mode |
|
|
229
|
+
| `siteId` | `string` | - | Site ID for permissions |
|
|
230
|
+
| `siteDomain` | `string` | - | Site domain for permissions |
|
|
231
|
+
| `globalMiddleware` | `boolean` | `false` | Enable auth on all routes |
|
|
232
|
+
| `publicRoutes` | `string[]` | `['/']` | Routes that don't require auth |
|
|
233
|
+
| `loginPath` | `string` | `'/login'` | Custom login page path |
|
|
234
|
+
| `enableDevTools` | `boolean` | `true` | Enable dev tools panel |
|
|
235
|
+
| `devToolsKey` | `string` | `'ctrl+shift+d'` | Keyboard shortcut |
|
|
236
|
+
| `cookieDomain` | `string` | - | Cookie domain override |
|
|
237
|
+
| `debug` | `boolean` | `false` | Enable debug logging |
|
|
238
|
+
|
|
239
|
+
### CobrasUser Type
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
interface CobrasUser {
|
|
243
|
+
id: string
|
|
244
|
+
email: string
|
|
245
|
+
name: string
|
|
246
|
+
role: 'admin' | 'user'
|
|
247
|
+
canAccessAdmin?: boolean
|
|
248
|
+
isAutoAuth?: boolean
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Examples
|
|
253
|
+
|
|
254
|
+
### Conditional Feature Based on Auth
|
|
255
|
+
|
|
256
|
+
```vue
|
|
257
|
+
<script setup>
|
|
258
|
+
const { isAuthenticated, isAdmin } = useCobrasAuth()
|
|
259
|
+
const { getFeatureFlag } = useCobrasDevTools()
|
|
260
|
+
|
|
261
|
+
const showBetaFeature = computed(() => {
|
|
262
|
+
// Show to admins, or authenticated users with flag enabled
|
|
263
|
+
return isAdmin.value || (isAuthenticated.value && getFeatureFlag('beta-feature'))
|
|
264
|
+
})
|
|
265
|
+
</script>
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Protected API Route
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
// server/api/admin/stats.get.ts
|
|
272
|
+
export default defineEventHandler(async (event) => {
|
|
273
|
+
// Verify auth via the proxy endpoint
|
|
274
|
+
const auth = await $fetch('/api/_cobras/verify', {
|
|
275
|
+
headers: { cookie: getHeader(event, 'cookie') || '' }
|
|
276
|
+
}).catch(() => null)
|
|
277
|
+
|
|
278
|
+
if (!auth?.valid || auth.user?.role !== 'admin') {
|
|
279
|
+
throw createError({ statusCode: 403, message: 'Admin access required' })
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return { stats: '...' }
|
|
283
|
+
})
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Development
|
|
287
|
+
|
|
288
|
+
```bash
|
|
289
|
+
# Install dependencies
|
|
290
|
+
pnpm install
|
|
291
|
+
|
|
292
|
+
# Build the module
|
|
293
|
+
pnpm prepack
|
|
294
|
+
|
|
295
|
+
# Dev with playground
|
|
296
|
+
pnpm dev:prepare
|
|
297
|
+
pnpm dev
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## License
|
|
301
|
+
|
|
302
|
+
MIT
|
package/dist/module.cjs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { Ref, ComputedRef } from 'vue';
|
|
3
|
+
|
|
4
|
+
interface CobrasUser {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
name: string;
|
|
8
|
+
role: 'admin' | 'user';
|
|
9
|
+
canAccessAdmin?: boolean;
|
|
10
|
+
isAutoAuth?: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface CobrasAuthState {
|
|
13
|
+
/** Current authenticated user, null if not logged in */
|
|
14
|
+
user: CobrasUser | null;
|
|
15
|
+
/** Whether auth state has been checked */
|
|
16
|
+
initialized: boolean;
|
|
17
|
+
/** Whether a check is in progress */
|
|
18
|
+
loading: boolean;
|
|
19
|
+
/** Last error if any */
|
|
20
|
+
error: string | null;
|
|
21
|
+
}
|
|
22
|
+
type AuthMode = 'internal' | 'public';
|
|
23
|
+
interface ModuleOptions {
|
|
24
|
+
/**
|
|
25
|
+
* URL of the Cobras auth service
|
|
26
|
+
* @default 'https://cobras-auth-app-production.up.railway.app'
|
|
27
|
+
*/
|
|
28
|
+
authServiceUrl: string;
|
|
29
|
+
/**
|
|
30
|
+
* Authentication mode:
|
|
31
|
+
* - 'internal': Full SSO - all protected routes require authentication
|
|
32
|
+
* - 'public': Public site with optional auth for special features/dev tools
|
|
33
|
+
* @default 'public'
|
|
34
|
+
*/
|
|
35
|
+
mode: AuthMode;
|
|
36
|
+
/**
|
|
37
|
+
* Site ID registered in cobras-auth (for site-specific permissions)
|
|
38
|
+
*/
|
|
39
|
+
siteId?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Site domain registered in cobras-auth (alternative to siteId)
|
|
42
|
+
*/
|
|
43
|
+
siteDomain?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Enable auth middleware on all routes by default
|
|
46
|
+
* In 'internal' mode, this protects all routes
|
|
47
|
+
* In 'public' mode, this just checks auth status silently
|
|
48
|
+
* @default false
|
|
49
|
+
*/
|
|
50
|
+
globalMiddleware: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Routes that don't require authentication (only applies in 'internal' mode)
|
|
53
|
+
* @default ['/']
|
|
54
|
+
*/
|
|
55
|
+
publicRoutes: string[];
|
|
56
|
+
/**
|
|
57
|
+
* Custom login page path (if you have one locally)
|
|
58
|
+
* Otherwise redirects to auth service
|
|
59
|
+
* @default '/login'
|
|
60
|
+
*/
|
|
61
|
+
loginPath: string;
|
|
62
|
+
/**
|
|
63
|
+
* Enable dev tools panel for authenticated users (public mode)
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
enableDevTools: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Keyboard shortcut to toggle dev tools
|
|
69
|
+
* @default 'ctrl+shift+d'
|
|
70
|
+
*/
|
|
71
|
+
devToolsKey: string;
|
|
72
|
+
/**
|
|
73
|
+
* Cookie domain for auth cookies (usually set automatically)
|
|
74
|
+
*/
|
|
75
|
+
cookieDomain?: string;
|
|
76
|
+
/**
|
|
77
|
+
* Enable debug logging
|
|
78
|
+
* @default false
|
|
79
|
+
*/
|
|
80
|
+
debug: boolean;
|
|
81
|
+
}
|
|
82
|
+
declare module '@nuxt/schema' {
|
|
83
|
+
interface RuntimeConfig {
|
|
84
|
+
cobrasAuth: {
|
|
85
|
+
cookieDomain?: string;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
interface PublicRuntimeConfig {
|
|
89
|
+
cobrasAuth: {
|
|
90
|
+
authServiceUrl: string;
|
|
91
|
+
mode: AuthMode;
|
|
92
|
+
siteId?: string;
|
|
93
|
+
siteDomain?: string;
|
|
94
|
+
publicRoutes: string[];
|
|
95
|
+
loginPath: string;
|
|
96
|
+
enableDevTools: boolean;
|
|
97
|
+
devToolsKey: string;
|
|
98
|
+
debug: boolean;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
declare module '#app' {
|
|
103
|
+
interface NuxtApp {
|
|
104
|
+
$cobrasAuth: {
|
|
105
|
+
user: Ref<CobrasUser | null>;
|
|
106
|
+
isAuthenticated: ComputedRef<boolean>;
|
|
107
|
+
isInternalUser: ComputedRef<boolean>;
|
|
108
|
+
isAdmin: ComputedRef<boolean>;
|
|
109
|
+
mode: AuthMode;
|
|
110
|
+
checkAuth: () => Promise<void>;
|
|
111
|
+
login: (redirect?: string) => void;
|
|
112
|
+
logout: () => Promise<void>;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
declare module 'vue' {
|
|
117
|
+
interface ComponentCustomProperties {
|
|
118
|
+
$cobrasAuth: {
|
|
119
|
+
user: Ref<CobrasUser | null>;
|
|
120
|
+
isAuthenticated: ComputedRef<boolean>;
|
|
121
|
+
isInternalUser: ComputedRef<boolean>;
|
|
122
|
+
isAdmin: ComputedRef<boolean>;
|
|
123
|
+
mode: AuthMode;
|
|
124
|
+
checkAuth: () => Promise<void>;
|
|
125
|
+
login: (redirect?: string) => void;
|
|
126
|
+
logout: () => Promise<void>;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
132
|
+
|
|
133
|
+
export { _default as default };
|
|
134
|
+
export type { CobrasAuthState, CobrasUser, ModuleOptions };
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { Ref, ComputedRef } from 'vue';
|
|
3
|
+
|
|
4
|
+
interface CobrasUser {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
name: string;
|
|
8
|
+
role: 'admin' | 'user';
|
|
9
|
+
canAccessAdmin?: boolean;
|
|
10
|
+
isAutoAuth?: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface CobrasAuthState {
|
|
13
|
+
/** Current authenticated user, null if not logged in */
|
|
14
|
+
user: CobrasUser | null;
|
|
15
|
+
/** Whether auth state has been checked */
|
|
16
|
+
initialized: boolean;
|
|
17
|
+
/** Whether a check is in progress */
|
|
18
|
+
loading: boolean;
|
|
19
|
+
/** Last error if any */
|
|
20
|
+
error: string | null;
|
|
21
|
+
}
|
|
22
|
+
type AuthMode = 'internal' | 'public';
|
|
23
|
+
interface ModuleOptions {
|
|
24
|
+
/**
|
|
25
|
+
* URL of the Cobras auth service
|
|
26
|
+
* @default 'https://cobras-auth-app-production.up.railway.app'
|
|
27
|
+
*/
|
|
28
|
+
authServiceUrl: string;
|
|
29
|
+
/**
|
|
30
|
+
* Authentication mode:
|
|
31
|
+
* - 'internal': Full SSO - all protected routes require authentication
|
|
32
|
+
* - 'public': Public site with optional auth for special features/dev tools
|
|
33
|
+
* @default 'public'
|
|
34
|
+
*/
|
|
35
|
+
mode: AuthMode;
|
|
36
|
+
/**
|
|
37
|
+
* Site ID registered in cobras-auth (for site-specific permissions)
|
|
38
|
+
*/
|
|
39
|
+
siteId?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Site domain registered in cobras-auth (alternative to siteId)
|
|
42
|
+
*/
|
|
43
|
+
siteDomain?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Enable auth middleware on all routes by default
|
|
46
|
+
* In 'internal' mode, this protects all routes
|
|
47
|
+
* In 'public' mode, this just checks auth status silently
|
|
48
|
+
* @default false
|
|
49
|
+
*/
|
|
50
|
+
globalMiddleware: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Routes that don't require authentication (only applies in 'internal' mode)
|
|
53
|
+
* @default ['/']
|
|
54
|
+
*/
|
|
55
|
+
publicRoutes: string[];
|
|
56
|
+
/**
|
|
57
|
+
* Custom login page path (if you have one locally)
|
|
58
|
+
* Otherwise redirects to auth service
|
|
59
|
+
* @default '/login'
|
|
60
|
+
*/
|
|
61
|
+
loginPath: string;
|
|
62
|
+
/**
|
|
63
|
+
* Enable dev tools panel for authenticated users (public mode)
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
enableDevTools: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Keyboard shortcut to toggle dev tools
|
|
69
|
+
* @default 'ctrl+shift+d'
|
|
70
|
+
*/
|
|
71
|
+
devToolsKey: string;
|
|
72
|
+
/**
|
|
73
|
+
* Cookie domain for auth cookies (usually set automatically)
|
|
74
|
+
*/
|
|
75
|
+
cookieDomain?: string;
|
|
76
|
+
/**
|
|
77
|
+
* Enable debug logging
|
|
78
|
+
* @default false
|
|
79
|
+
*/
|
|
80
|
+
debug: boolean;
|
|
81
|
+
}
|
|
82
|
+
declare module '@nuxt/schema' {
|
|
83
|
+
interface RuntimeConfig {
|
|
84
|
+
cobrasAuth: {
|
|
85
|
+
cookieDomain?: string;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
interface PublicRuntimeConfig {
|
|
89
|
+
cobrasAuth: {
|
|
90
|
+
authServiceUrl: string;
|
|
91
|
+
mode: AuthMode;
|
|
92
|
+
siteId?: string;
|
|
93
|
+
siteDomain?: string;
|
|
94
|
+
publicRoutes: string[];
|
|
95
|
+
loginPath: string;
|
|
96
|
+
enableDevTools: boolean;
|
|
97
|
+
devToolsKey: string;
|
|
98
|
+
debug: boolean;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
declare module '#app' {
|
|
103
|
+
interface NuxtApp {
|
|
104
|
+
$cobrasAuth: {
|
|
105
|
+
user: Ref<CobrasUser | null>;
|
|
106
|
+
isAuthenticated: ComputedRef<boolean>;
|
|
107
|
+
isInternalUser: ComputedRef<boolean>;
|
|
108
|
+
isAdmin: ComputedRef<boolean>;
|
|
109
|
+
mode: AuthMode;
|
|
110
|
+
checkAuth: () => Promise<void>;
|
|
111
|
+
login: (redirect?: string) => void;
|
|
112
|
+
logout: () => Promise<void>;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
declare module 'vue' {
|
|
117
|
+
interface ComponentCustomProperties {
|
|
118
|
+
$cobrasAuth: {
|
|
119
|
+
user: Ref<CobrasUser | null>;
|
|
120
|
+
isAuthenticated: ComputedRef<boolean>;
|
|
121
|
+
isInternalUser: ComputedRef<boolean>;
|
|
122
|
+
isAdmin: ComputedRef<boolean>;
|
|
123
|
+
mode: AuthMode;
|
|
124
|
+
checkAuth: () => Promise<void>;
|
|
125
|
+
login: (redirect?: string) => void;
|
|
126
|
+
logout: () => Promise<void>;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
132
|
+
|
|
133
|
+
export { _default as default };
|
|
134
|
+
export type { CobrasAuthState, CobrasUser, ModuleOptions };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { defineNuxtModule, createResolver, addPlugin, addImports, addRouteMiddleware, addServerHandler, addComponent } from '@nuxt/kit';
|
|
2
|
+
import { defu } from 'defu';
|
|
3
|
+
|
|
4
|
+
const module = defineNuxtModule({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "@cobras/auth-nuxt",
|
|
7
|
+
configKey: "cobrasAuth",
|
|
8
|
+
compatibility: {
|
|
9
|
+
nuxt: ">=3.0.0"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
defaults: {
|
|
13
|
+
authServiceUrl: "https://cobras-auth-app-production.up.railway.app",
|
|
14
|
+
mode: "public",
|
|
15
|
+
siteId: void 0,
|
|
16
|
+
siteDomain: void 0,
|
|
17
|
+
globalMiddleware: false,
|
|
18
|
+
publicRoutes: [],
|
|
19
|
+
// Empty by default - user must explicitly set public routes
|
|
20
|
+
loginPath: "/login",
|
|
21
|
+
enableDevTools: true,
|
|
22
|
+
devToolsKey: "ctrl+shift+d",
|
|
23
|
+
cookieDomain: void 0,
|
|
24
|
+
debug: false
|
|
25
|
+
},
|
|
26
|
+
setup(options, nuxt) {
|
|
27
|
+
const resolver = createResolver(import.meta.url);
|
|
28
|
+
nuxt.options.runtimeConfig.public.cobrasAuth = defu(
|
|
29
|
+
nuxt.options.runtimeConfig.public.cobrasAuth || {},
|
|
30
|
+
{
|
|
31
|
+
authServiceUrl: options.authServiceUrl,
|
|
32
|
+
mode: options.mode,
|
|
33
|
+
siteId: options.siteId,
|
|
34
|
+
siteDomain: options.siteDomain,
|
|
35
|
+
publicRoutes: options.publicRoutes,
|
|
36
|
+
loginPath: options.loginPath,
|
|
37
|
+
enableDevTools: options.enableDevTools,
|
|
38
|
+
devToolsKey: options.devToolsKey,
|
|
39
|
+
debug: options.debug
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
nuxt.options.runtimeConfig.cobrasAuth = defu(
|
|
43
|
+
nuxt.options.runtimeConfig.cobrasAuth || {},
|
|
44
|
+
{
|
|
45
|
+
cookieDomain: options.cookieDomain
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
addPlugin({
|
|
49
|
+
src: resolver.resolve("./runtime/plugins/auth.client"),
|
|
50
|
+
mode: "client"
|
|
51
|
+
});
|
|
52
|
+
addPlugin({
|
|
53
|
+
src: resolver.resolve("./runtime/plugins/auth.server"),
|
|
54
|
+
mode: "server"
|
|
55
|
+
});
|
|
56
|
+
addImports([
|
|
57
|
+
{
|
|
58
|
+
name: "useCobrasAuth",
|
|
59
|
+
from: resolver.resolve("./runtime/composables/useCobrasAuth")
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "useCobrasDevTools",
|
|
63
|
+
from: resolver.resolve("./runtime/composables/useCobrasDevTools")
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: "useCobrasMode",
|
|
67
|
+
from: resolver.resolve("./runtime/composables/useCobrasMode")
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
addRouteMiddleware({
|
|
71
|
+
name: "cobras-auth",
|
|
72
|
+
path: resolver.resolve("./runtime/middleware/auth"),
|
|
73
|
+
global: options.globalMiddleware
|
|
74
|
+
});
|
|
75
|
+
addRouteMiddleware({
|
|
76
|
+
name: "cobras-internal",
|
|
77
|
+
path: resolver.resolve("./runtime/middleware/internal"),
|
|
78
|
+
global: false
|
|
79
|
+
});
|
|
80
|
+
addServerHandler({
|
|
81
|
+
route: "/api/_cobras/verify",
|
|
82
|
+
handler: resolver.resolve("./runtime/server/api/verify.get")
|
|
83
|
+
});
|
|
84
|
+
addServerHandler({
|
|
85
|
+
route: "/api/_cobras/refresh",
|
|
86
|
+
handler: resolver.resolve("./runtime/server/api/refresh.post")
|
|
87
|
+
});
|
|
88
|
+
addServerHandler({
|
|
89
|
+
route: "/api/_cobras/logout",
|
|
90
|
+
handler: resolver.resolve("./runtime/server/api/logout.post")
|
|
91
|
+
});
|
|
92
|
+
addServerHandler({
|
|
93
|
+
route: "/api/_cobras/exchange",
|
|
94
|
+
handler: resolver.resolve("./runtime/server/api/exchange.post")
|
|
95
|
+
});
|
|
96
|
+
if (options.enableDevTools) {
|
|
97
|
+
addComponent({
|
|
98
|
+
name: "CobrasDevTools",
|
|
99
|
+
filePath: resolver.resolve("./runtime/components/CobrasDevTools.vue")
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (options.debug) {
|
|
103
|
+
console.log("[@cobras/auth-nuxt] Module configured:", {
|
|
104
|
+
mode: options.mode,
|
|
105
|
+
authServiceUrl: options.authServiceUrl,
|
|
106
|
+
globalMiddleware: options.globalMiddleware
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
export { module as default };
|