create-nuxt-base 0.3.17 → 1.0.2
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/.github/workflows/publish.yml +4 -2
- package/.oxfmtrc.jsonc +7 -0
- package/CHANGELOG.md +20 -8
- package/nuxt-base-template/.dockerignore +44 -0
- package/nuxt-base-template/.nuxtrc +1 -0
- package/nuxt-base-template/.oxfmtrc.jsonc +8 -0
- package/nuxt-base-template/Dockerfile.dev +23 -0
- package/nuxt-base-template/app/components/Modal/ModalBackupCodes.vue +117 -0
- package/nuxt-base-template/app/components/Upload/TusFileUpload.vue +302 -0
- package/nuxt-base-template/app/composables/use-better-auth.ts +25 -0
- package/nuxt-base-template/app/composables/use-file.ts +39 -4
- package/nuxt-base-template/app/composables/use-share.ts +1 -1
- package/nuxt-base-template/app/composables/use-tus-upload.ts +278 -0
- package/nuxt-base-template/app/interfaces/upload.interface.ts +58 -0
- package/nuxt-base-template/app/interfaces/user.interface.ts +12 -0
- package/nuxt-base-template/app/lib/auth-client.ts +135 -0
- package/nuxt-base-template/app/middleware/admin.global.ts +23 -0
- package/nuxt-base-template/app/middleware/auth.global.ts +18 -0
- package/nuxt-base-template/app/middleware/guest.global.ts +18 -0
- package/nuxt-base-template/app/pages/app/settings/security.vue +409 -0
- package/nuxt-base-template/app/pages/auth/2fa.vue +120 -0
- package/nuxt-base-template/app/pages/auth/forgot-password.vue +72 -21
- package/nuxt-base-template/app/pages/auth/login.vue +75 -11
- package/nuxt-base-template/app/pages/auth/register.vue +184 -0
- package/nuxt-base-template/app/pages/auth/reset-password.vue +153 -0
- package/nuxt-base-template/app/utils/crypto.ts +13 -0
- package/nuxt-base-template/docker-entrypoint.sh +21 -0
- package/nuxt-base-template/nuxt.config.ts +4 -1
- package/nuxt-base-template/oxlint.json +14 -0
- package/nuxt-base-template/package-lock.json +11582 -10675
- package/nuxt-base-template/package.json +35 -32
- package/nuxt-base-template/tests/iam.spec.ts +247 -0
- package/package.json +14 -11
- package/.eslintignore +0 -14
- package/.eslintrc +0 -3
- package/.prettierignore +0 -5
- package/.prettierrc +0 -6
- package/nuxt-base-template/CLAUDE.md +0 -361
- package/nuxt-base-template/app/pages/auth/reset-password/[token].vue +0 -110
- package/nuxt-base-template/app/public/favicon.ico +0 -0
- package/nuxt-base-template/eslint.config.mjs +0 -4
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import * as tus from 'tus-js-client';
|
|
2
|
+
|
|
3
|
+
import type { UploadItem, UploadOptions, UploadProgress, UseTusUploadReturn } from '~/interfaces/upload.interface';
|
|
4
|
+
|
|
5
|
+
export function useTusUpload(defaultOptions: UploadOptions = {}): UseTusUploadReturn {
|
|
6
|
+
const config = useRuntimeConfig();
|
|
7
|
+
|
|
8
|
+
// State
|
|
9
|
+
const uploadItems = ref<Map<string, UploadItem>>(new Map());
|
|
10
|
+
const tusUploads = ref<Map<string, tus.Upload>>(new Map());
|
|
11
|
+
|
|
12
|
+
// Default config
|
|
13
|
+
const defaultConfig: UploadOptions = {
|
|
14
|
+
autoStart: true,
|
|
15
|
+
chunkSize: 5 * 1024 * 1024, // 5MB chunks
|
|
16
|
+
endpoint: `${config.public.host}/files/upload`,
|
|
17
|
+
parallelUploads: 3,
|
|
18
|
+
retryDelays: [0, 1000, 3000, 5000, 10000],
|
|
19
|
+
...defaultOptions,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Computed
|
|
23
|
+
const uploads = computed(() => Array.from(uploadItems.value.values()));
|
|
24
|
+
const isUploading = computed(() => uploads.value.some((u) => u.status === 'uploading'));
|
|
25
|
+
const totalProgress = computed<UploadProgress>(() => {
|
|
26
|
+
const items = uploads.value;
|
|
27
|
+
if (items.length === 0) {
|
|
28
|
+
return { bytesTotal: 0, bytesUploaded: 0, percentage: 0, remainingTime: 0, speed: 0 };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const bytesUploaded = items.reduce((acc, i) => acc + i.progress.bytesUploaded, 0);
|
|
32
|
+
const bytesTotal = items.reduce((acc, i) => acc + i.progress.bytesTotal, 0);
|
|
33
|
+
const speed = items.reduce((acc, i) => acc + i.progress.speed, 0);
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
bytesTotal,
|
|
37
|
+
bytesUploaded,
|
|
38
|
+
percentage: bytesTotal > 0 ? Math.round((bytesUploaded / bytesTotal) * 100) : 0,
|
|
39
|
+
remainingTime: speed > 0 ? Math.ceil((bytesTotal - bytesUploaded) / speed) : 0,
|
|
40
|
+
speed,
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Helper: Generate unique ID
|
|
45
|
+
function generateId(): string {
|
|
46
|
+
return `upload_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Helper: Calculate speed with smoothing
|
|
50
|
+
function createSpeedTracker() {
|
|
51
|
+
let lastBytes = 0;
|
|
52
|
+
let lastTime = Date.now();
|
|
53
|
+
let smoothedSpeed = 0;
|
|
54
|
+
|
|
55
|
+
return (bytesUploaded: number): number => {
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
const timeDiff = (now - lastTime) / 1000;
|
|
58
|
+
const bytesDiff = bytesUploaded - lastBytes;
|
|
59
|
+
|
|
60
|
+
if (timeDiff > 0) {
|
|
61
|
+
const currentSpeed = bytesDiff / timeDiff;
|
|
62
|
+
// Exponential moving average for smoother display
|
|
63
|
+
smoothedSpeed = smoothedSpeed === 0 ? currentSpeed : smoothedSpeed * 0.7 + currentSpeed * 0.3;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
lastBytes = bytesUploaded;
|
|
67
|
+
lastTime = now;
|
|
68
|
+
|
|
69
|
+
return Math.round(smoothedSpeed);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Update item in map (triggers reactivity)
|
|
74
|
+
function updateItem(id: string, updates: Partial<UploadItem>): void {
|
|
75
|
+
const item = uploadItems.value.get(id);
|
|
76
|
+
if (item) {
|
|
77
|
+
const newMap = new Map(uploadItems.value);
|
|
78
|
+
newMap.set(id, { ...item, ...updates });
|
|
79
|
+
uploadItems.value = newMap;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Create TUS upload instance
|
|
84
|
+
function createTusUpload(item: UploadItem, options: UploadOptions): tus.Upload {
|
|
85
|
+
const speedTracker = createSpeedTracker();
|
|
86
|
+
|
|
87
|
+
return new tus.Upload(item.file, {
|
|
88
|
+
chunkSize: options.chunkSize || defaultConfig.chunkSize,
|
|
89
|
+
endpoint: options.endpoint || defaultConfig.endpoint,
|
|
90
|
+
headers: options.headers,
|
|
91
|
+
metadata: {
|
|
92
|
+
filename: item.file.name,
|
|
93
|
+
filetype: item.file.type,
|
|
94
|
+
...options.metadata,
|
|
95
|
+
...item.metadata,
|
|
96
|
+
},
|
|
97
|
+
onBeforeRequest: (req) => {
|
|
98
|
+
const xhr = req.getUnderlyingObject() as XMLHttpRequest;
|
|
99
|
+
xhr.withCredentials = true;
|
|
100
|
+
},
|
|
101
|
+
onError: (error) => {
|
|
102
|
+
updateItem(item.id, {
|
|
103
|
+
error: error.message,
|
|
104
|
+
status: 'error',
|
|
105
|
+
});
|
|
106
|
+
options.onError?.(uploadItems.value.get(item.id)!, error);
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
onProgress: (bytesUploaded, bytesTotal) => {
|
|
110
|
+
const speed = speedTracker(bytesUploaded);
|
|
111
|
+
const percentage = Math.round((bytesUploaded / bytesTotal) * 100);
|
|
112
|
+
const remainingTime = speed > 0 ? Math.ceil((bytesTotal - bytesUploaded) / speed) : 0;
|
|
113
|
+
|
|
114
|
+
updateItem(item.id, {
|
|
115
|
+
progress: { bytesTotal, bytesUploaded, percentage, remainingTime, speed },
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
options.onProgress?.(uploadItems.value.get(item.id)!);
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
onShouldRetry: (err) => {
|
|
122
|
+
const status = (err as { originalResponse?: { getStatus?: () => number } }).originalResponse?.getStatus?.();
|
|
123
|
+
// Don't retry on 4xx errors (except 429 Too Many Requests)
|
|
124
|
+
if (status && status >= 400 && status < 500 && status !== 429) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
onSuccess: () => {
|
|
131
|
+
const tusUpload = tusUploads.value.get(item.id);
|
|
132
|
+
const currentItem = uploadItems.value.get(item.id);
|
|
133
|
+
updateItem(item.id, {
|
|
134
|
+
completedAt: new Date(),
|
|
135
|
+
progress: { ...currentItem!.progress, percentage: 100 },
|
|
136
|
+
status: 'completed',
|
|
137
|
+
url: tusUpload?.url ?? undefined,
|
|
138
|
+
});
|
|
139
|
+
options.onSuccess?.(uploadItems.value.get(item.id)!);
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
retryDelays: options.retryDelays || defaultConfig.retryDelays,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Actions
|
|
147
|
+
function addFiles(files: File | File[]): string[] {
|
|
148
|
+
const fileArray = Array.isArray(files) ? files : [files];
|
|
149
|
+
const ids: string[] = [];
|
|
150
|
+
|
|
151
|
+
for (const file of fileArray) {
|
|
152
|
+
const id = generateId();
|
|
153
|
+
const item: UploadItem = {
|
|
154
|
+
file,
|
|
155
|
+
id,
|
|
156
|
+
metadata: defaultConfig.metadata,
|
|
157
|
+
progress: { bytesTotal: file.size, bytesUploaded: 0, percentage: 0, remainingTime: 0, speed: 0 },
|
|
158
|
+
status: 'idle',
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const newMap = new Map(uploadItems.value);
|
|
162
|
+
newMap.set(id, item);
|
|
163
|
+
uploadItems.value = newMap;
|
|
164
|
+
|
|
165
|
+
const tusUpload = createTusUpload(item, defaultConfig);
|
|
166
|
+
tusUploads.value.set(id, tusUpload);
|
|
167
|
+
|
|
168
|
+
ids.push(id);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (defaultConfig.autoStart) {
|
|
172
|
+
startAll();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return ids;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function startUpload(id: string): void {
|
|
179
|
+
const item = uploadItems.value.get(id);
|
|
180
|
+
const tusUpload = tusUploads.value.get(id);
|
|
181
|
+
|
|
182
|
+
if (item && tusUpload && item.status !== 'uploading') {
|
|
183
|
+
updateItem(id, { startedAt: new Date(), status: 'uploading' });
|
|
184
|
+
|
|
185
|
+
// Check for previous uploads to resume
|
|
186
|
+
tusUpload.findPreviousUploads().then((previousUploads) => {
|
|
187
|
+
const previousUpload = previousUploads[0];
|
|
188
|
+
if (previousUpload) {
|
|
189
|
+
tusUpload.resumeFromPreviousUpload(previousUpload);
|
|
190
|
+
}
|
|
191
|
+
tusUpload.start();
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function startAll(): void {
|
|
197
|
+
const pending = uploads.value.filter((u) => u.status === 'idle' || u.status === 'paused');
|
|
198
|
+
const currentlyUploading = uploads.value.filter((u) => u.status === 'uploading').length;
|
|
199
|
+
const limit = (defaultConfig.parallelUploads || 3) - currentlyUploading;
|
|
200
|
+
|
|
201
|
+
pending.slice(0, Math.max(0, limit)).forEach((item) => startUpload(item.id));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function pauseUpload(id: string): void {
|
|
205
|
+
const tusUpload = tusUploads.value.get(id);
|
|
206
|
+
if (tusUpload) {
|
|
207
|
+
tusUpload.abort();
|
|
208
|
+
updateItem(id, { status: 'paused' });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function pauseAll(): void {
|
|
213
|
+
uploads.value.filter((u) => u.status === 'uploading').forEach((item) => pauseUpload(item.id));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function resumeUpload(id: string): void {
|
|
217
|
+
startUpload(id);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function resumeAll(): void {
|
|
221
|
+
uploads.value.filter((u) => u.status === 'paused').forEach((item) => resumeUpload(item.id));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function cancelUpload(id: string): void {
|
|
225
|
+
const tusUpload = tusUploads.value.get(id);
|
|
226
|
+
if (tusUpload) {
|
|
227
|
+
tusUpload.abort();
|
|
228
|
+
}
|
|
229
|
+
tusUploads.value.delete(id);
|
|
230
|
+
|
|
231
|
+
const newMap = new Map(uploadItems.value);
|
|
232
|
+
newMap.delete(id);
|
|
233
|
+
uploadItems.value = newMap;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function cancelAll(): void {
|
|
237
|
+
uploads.value.forEach((item) => cancelUpload(item.id));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function removeUpload(id: string): void {
|
|
241
|
+
cancelUpload(id);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function clearCompleted(): void {
|
|
245
|
+
uploads.value.filter((u) => u.status === 'completed').forEach((item) => removeUpload(item.id));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function retryUpload(id: string): void {
|
|
249
|
+
const item = uploadItems.value.get(id);
|
|
250
|
+
if (item && item.status === 'error') {
|
|
251
|
+
updateItem(id, { error: undefined, status: 'idle' });
|
|
252
|
+
startUpload(id);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function getUpload(id: string): undefined | UploadItem {
|
|
257
|
+
return uploadItems.value.get(id);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
addFiles,
|
|
262
|
+
cancelAll,
|
|
263
|
+
cancelUpload,
|
|
264
|
+
clearCompleted,
|
|
265
|
+
getUpload,
|
|
266
|
+
isUploading,
|
|
267
|
+
pauseAll,
|
|
268
|
+
pauseUpload,
|
|
269
|
+
removeUpload,
|
|
270
|
+
resumeAll,
|
|
271
|
+
resumeUpload,
|
|
272
|
+
retryUpload,
|
|
273
|
+
startAll,
|
|
274
|
+
startUpload,
|
|
275
|
+
totalProgress,
|
|
276
|
+
uploads,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ComputedRef } from 'vue';
|
|
2
|
+
|
|
3
|
+
export interface UploadItem {
|
|
4
|
+
completedAt?: Date;
|
|
5
|
+
error?: string;
|
|
6
|
+
file: File;
|
|
7
|
+
id: string;
|
|
8
|
+
metadata?: Record<string, string>;
|
|
9
|
+
progress: UploadProgress;
|
|
10
|
+
startedAt?: Date;
|
|
11
|
+
status: UploadStatus;
|
|
12
|
+
url?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface UploadOptions {
|
|
16
|
+
autoStart?: boolean;
|
|
17
|
+
chunkSize?: number;
|
|
18
|
+
endpoint?: string;
|
|
19
|
+
headers?: Record<string, string>;
|
|
20
|
+
metadata?: Record<string, string>;
|
|
21
|
+
onError?: (item: UploadItem, error: Error) => void;
|
|
22
|
+
onProgress?: (item: UploadItem) => void;
|
|
23
|
+
onSuccess?: (item: UploadItem) => void;
|
|
24
|
+
parallelUploads?: number;
|
|
25
|
+
retryDelays?: number[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface UploadProgress {
|
|
29
|
+
bytesTotal: number;
|
|
30
|
+
bytesUploaded: number;
|
|
31
|
+
percentage: number;
|
|
32
|
+
remainingTime: number; // seconds
|
|
33
|
+
speed: number; // bytes/second
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type UploadStatus = 'completed' | 'error' | 'idle' | 'paused' | 'uploading';
|
|
37
|
+
|
|
38
|
+
export interface UseTusUploadReturn {
|
|
39
|
+
// Actions
|
|
40
|
+
addFiles: (files: File | File[]) => string[];
|
|
41
|
+
cancelAll: () => void;
|
|
42
|
+
cancelUpload: (id: string) => void;
|
|
43
|
+
clearCompleted: () => void;
|
|
44
|
+
getUpload: (id: string) => undefined | UploadItem;
|
|
45
|
+
|
|
46
|
+
// State
|
|
47
|
+
isUploading: ComputedRef<boolean>;
|
|
48
|
+
pauseAll: () => void;
|
|
49
|
+
pauseUpload: (id: string) => void;
|
|
50
|
+
removeUpload: (id: string) => void;
|
|
51
|
+
resumeAll: () => void;
|
|
52
|
+
resumeUpload: (id: string) => void;
|
|
53
|
+
retryUpload: (id: string) => void;
|
|
54
|
+
startAll: () => void;
|
|
55
|
+
startUpload: (id: string) => void;
|
|
56
|
+
totalProgress: ComputedRef<UploadProgress>;
|
|
57
|
+
uploads: ComputedRef<UploadItem[]>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { passkeyClient } from '@better-auth/passkey/client';
|
|
2
|
+
import { adminClient, twoFactorClient } from 'better-auth/client/plugins';
|
|
3
|
+
import { createAuthClient } from 'better-auth/vue';
|
|
4
|
+
|
|
5
|
+
import { sha256 } from '~/utils/crypto';
|
|
6
|
+
|
|
7
|
+
// =============================================================================
|
|
8
|
+
// Type Definitions
|
|
9
|
+
// =============================================================================
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Normalized response type for Better-Auth operations
|
|
13
|
+
* The Vue client returns complex union types - this provides a consistent interface
|
|
14
|
+
*/
|
|
15
|
+
export interface AuthResponse {
|
|
16
|
+
data?: null | {
|
|
17
|
+
redirect?: boolean;
|
|
18
|
+
token?: null | string;
|
|
19
|
+
url?: string;
|
|
20
|
+
user?: {
|
|
21
|
+
createdAt?: Date;
|
|
22
|
+
email?: string;
|
|
23
|
+
emailVerified?: boolean;
|
|
24
|
+
id?: string;
|
|
25
|
+
image?: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
updatedAt?: Date;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
error?: null | {
|
|
31
|
+
code?: string;
|
|
32
|
+
message?: string;
|
|
33
|
+
status?: number;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// =============================================================================
|
|
38
|
+
// Base Client Configuration
|
|
39
|
+
// =============================================================================
|
|
40
|
+
|
|
41
|
+
const baseClient = createAuthClient({
|
|
42
|
+
basePath: '/iam', // IMPORTANT: Must match nest-server betterAuth.basePath, default: '/iam'
|
|
43
|
+
baseURL: import.meta.env?.VITE_API_URL || process.env.API_URL || 'http://localhost:3000',
|
|
44
|
+
plugins: [
|
|
45
|
+
adminClient(),
|
|
46
|
+
twoFactorClient({
|
|
47
|
+
onTwoFactorRedirect() {
|
|
48
|
+
navigateTo('/auth/2fa');
|
|
49
|
+
},
|
|
50
|
+
}),
|
|
51
|
+
passkeyClient(),
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// =============================================================================
|
|
56
|
+
// Auth Client with Password Hashing
|
|
57
|
+
// =============================================================================
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Extended auth client that hashes passwords before transmission.
|
|
61
|
+
*
|
|
62
|
+
* SECURITY: Passwords are hashed with SHA256 client-side to prevent
|
|
63
|
+
* plain text password transmission over the network.
|
|
64
|
+
*
|
|
65
|
+
* The server's normalizePasswordForIam() detects SHA256 hashes (64 hex chars)
|
|
66
|
+
* and processes them correctly.
|
|
67
|
+
*/
|
|
68
|
+
export const authClient = {
|
|
69
|
+
// Spread all base client properties and methods
|
|
70
|
+
...baseClient,
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Change password for an authenticated user (both passwords are hashed)
|
|
74
|
+
*/
|
|
75
|
+
changePassword: async (params: { currentPassword: string; newPassword: string }, options?: any) => {
|
|
76
|
+
const [hashedCurrent, hashedNew] = await Promise.all([sha256(params.currentPassword), sha256(params.newPassword)]);
|
|
77
|
+
return baseClient.changePassword?.({ currentPassword: hashedCurrent, newPassword: hashedNew }, options);
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Reset password with token (new password is hashed before sending)
|
|
82
|
+
*/
|
|
83
|
+
resetPassword: async (params: { newPassword: string; token: string }, options?: any) => {
|
|
84
|
+
const hashedPassword = await sha256(params.newPassword);
|
|
85
|
+
return baseClient.resetPassword?.({ newPassword: hashedPassword, token: params.token }, options);
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
// Override signIn to hash password
|
|
89
|
+
signIn: {
|
|
90
|
+
...baseClient.signIn,
|
|
91
|
+
/**
|
|
92
|
+
* Sign in with email and password (password is hashed before sending)
|
|
93
|
+
*/
|
|
94
|
+
email: async (params: { email: string; password: string; rememberMe?: boolean }, options?: any) => {
|
|
95
|
+
const hashedPassword = await sha256(params.password);
|
|
96
|
+
return baseClient.signIn.email({ ...params, password: hashedPassword }, options);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
// Explicitly pass through signOut (not captured by spread operator)
|
|
101
|
+
signOut: baseClient.signOut,
|
|
102
|
+
|
|
103
|
+
// Override signUp to hash password
|
|
104
|
+
signUp: {
|
|
105
|
+
...baseClient.signUp,
|
|
106
|
+
/**
|
|
107
|
+
* Sign up with email and password (password is hashed before sending)
|
|
108
|
+
*/
|
|
109
|
+
email: async (params: { email: string; name: string; password: string }, options?: any) => {
|
|
110
|
+
const hashedPassword = await sha256(params.password);
|
|
111
|
+
return baseClient.signUp.email({ ...params, password: hashedPassword }, options);
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
// Override twoFactor to hash passwords
|
|
116
|
+
twoFactor: {
|
|
117
|
+
...baseClient.twoFactor,
|
|
118
|
+
/**
|
|
119
|
+
* Disable 2FA (password is hashed before sending)
|
|
120
|
+
*/
|
|
121
|
+
disable: async (params: { password: string }, options?: any) => {
|
|
122
|
+
const hashedPassword = await sha256(params.password);
|
|
123
|
+
return baseClient.twoFactor.disable({ password: hashedPassword }, options);
|
|
124
|
+
},
|
|
125
|
+
/**
|
|
126
|
+
* Enable 2FA (password is hashed before sending)
|
|
127
|
+
*/
|
|
128
|
+
enable: async (params: { password: string }, options?: any) => {
|
|
129
|
+
const hashedPassword = await sha256(params.password);
|
|
130
|
+
return baseClient.twoFactor.enable({ password: hashedPassword }, options);
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export type AuthClient = typeof authClient;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default defineNuxtRouteMiddleware(async (to) => {
|
|
2
|
+
// Only check routes starting with /app/admin
|
|
3
|
+
if (!to.path.startsWith('/app/admin')) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const { isAdmin, isAuthenticated, isLoading } = useBetterAuth();
|
|
8
|
+
|
|
9
|
+
// Wait for session to load
|
|
10
|
+
if (isLoading.value) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Redirect to login if not authenticated
|
|
15
|
+
if (!isAuthenticated.value) {
|
|
16
|
+
return navigateTo('/auth/login');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Redirect to /app if authenticated but not admin
|
|
20
|
+
if (!isAdmin.value) {
|
|
21
|
+
return navigateTo('/app');
|
|
22
|
+
}
|
|
23
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default defineNuxtRouteMiddleware(async (to) => {
|
|
2
|
+
// Only check routes starting with /app (but not /app/admin, handled by admin middleware)
|
|
3
|
+
if (!to.path.startsWith('/app') || to.path.startsWith('/app/admin')) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const { isAuthenticated, isLoading } = useBetterAuth();
|
|
8
|
+
|
|
9
|
+
// Wait for session to load
|
|
10
|
+
if (isLoading.value) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Redirect to login if not authenticated
|
|
15
|
+
if (!isAuthenticated.value) {
|
|
16
|
+
return navigateTo('/auth/login');
|
|
17
|
+
}
|
|
18
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default defineNuxtRouteMiddleware(async (to) => {
|
|
2
|
+
// Only check /auth/login route
|
|
3
|
+
if (to.path !== '/auth/login') {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const { isAuthenticated, isLoading } = useBetterAuth();
|
|
8
|
+
|
|
9
|
+
// Wait for session to load
|
|
10
|
+
if (isLoading.value) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Redirect to /app if already authenticated
|
|
15
|
+
if (isAuthenticated.value) {
|
|
16
|
+
return navigateTo('/app');
|
|
17
|
+
}
|
|
18
|
+
});
|