@strands.gg/accui 1.9.0 → 1.10.1
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/dist/accui.css +1 -5802
- package/dist/nuxt/module.cjs.js +1 -109
- package/dist/nuxt/module.cjs.js.map +1 -1
- package/dist/nuxt/module.es.js +9 -1
- package/dist/nuxt/module.es.js.map +1 -1
- package/dist/nuxt/runtime/composables/useStrandsAuth.cjs.js +1 -57
- package/dist/nuxt/runtime/composables/useStrandsAuth.cjs.js.map +1 -1
- package/dist/nuxt/runtime/composables/useStrandsAuth.es.js +1 -2
- package/dist/nuxt/runtime/middleware/auth.global.cjs.js +1 -42
- package/dist/nuxt/runtime/middleware/auth.global.cjs.js.map +1 -1
- package/dist/nuxt/runtime/middleware/auth.global.es.js +0 -1
- package/dist/nuxt/runtime/plugin.client.cjs.js +1 -26
- package/dist/nuxt/runtime/plugin.client.cjs.js.map +1 -1
- package/dist/nuxt/runtime/plugin.client.es.js +2 -1
- package/dist/nuxt/runtime/plugin.client.es.js.map +1 -1
- package/dist/nuxt/runtime/plugin.server.cjs.js +1 -17
- package/dist/nuxt/runtime/plugin.server.cjs.js.map +1 -1
- package/dist/nuxt/runtime/plugin.server.es.js +0 -1
- package/dist/nuxt-v4/module.cjs.js +1 -119
- package/dist/nuxt-v4/module.cjs.js.map +1 -1
- package/dist/nuxt-v4/module.es.js +9 -1
- package/dist/nuxt-v4/module.es.js.map +1 -1
- package/dist/nuxt-v4/runtime/composables/useStrandsAuth.cjs.js +1 -70
- package/dist/nuxt-v4/runtime/composables/useStrandsAuth.cjs.js.map +1 -1
- package/dist/nuxt-v4/runtime/composables/useStrandsAuth.es.js +1 -2
- package/dist/nuxt-v4/runtime/middleware/auth.global.cjs.js +1 -42
- package/dist/nuxt-v4/runtime/middleware/auth.global.cjs.js.map +1 -1
- package/dist/nuxt-v4/runtime/middleware/auth.global.es.js +0 -1
- package/dist/nuxt-v4/runtime/plugin.client.cjs.js +1 -26
- package/dist/nuxt-v4/runtime/plugin.client.cjs.js.map +1 -1
- package/dist/nuxt-v4/runtime/plugin.client.es.js +2 -1
- package/dist/nuxt-v4/runtime/plugin.client.es.js.map +1 -1
- package/dist/nuxt-v4/runtime/plugin.server.cjs.js +1 -22
- package/dist/nuxt-v4/runtime/plugin.server.cjs.js.map +1 -1
- package/dist/nuxt-v4/runtime/plugin.server.es.js +0 -1
- package/dist/strands-auth-ui.cjs.js +1 -9840
- package/dist/strands-auth-ui.cjs.js.map +1 -1
- package/dist/strands-auth-ui.es.js +1398 -808
- package/dist/strands-auth-ui.es.js.map +1 -1
- package/dist/useStrandsAuth-CTlaiFqK.cjs +1 -0
- package/dist/useStrandsAuth-CTlaiFqK.cjs.map +1 -0
- package/dist/{useStrandsAuth-BA8qEUcp.js → useStrandsAuth-Cev-PTun.js} +289 -127
- package/dist/useStrandsAuth-Cev-PTun.js.map +1 -0
- package/dist/useStrandsConfig-Cxb360Os.js +0 -1
- package/dist/useStrandsConfig-Z9_36OcV.cjs +1 -0
- package/dist/{useStrandsConfig-Dms13Zd0.cjs.map → useStrandsConfig-Z9_36OcV.cjs.map} +1 -1
- package/package.json +1 -1
- package/dist/useStrandsAuth-BA8qEUcp.js.map +0 -1
- package/dist/useStrandsAuth-Co2lLH4X.cjs +0 -736
- package/dist/useStrandsAuth-Co2lLH4X.cjs.map +0 -1
- package/dist/useStrandsConfig-Dms13Zd0.cjs +0 -179
|
@@ -1,736 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const vue = require("vue");
|
|
3
|
-
const useStrandsConfig = require("./useStrandsConfig-Dms13Zd0.cjs");
|
|
4
|
-
const mapApiUserToFrontend = (apiUser) => {
|
|
5
|
-
return {
|
|
6
|
-
id: apiUser.id,
|
|
7
|
-
email: apiUser.email,
|
|
8
|
-
firstName: apiUser.first_name || apiUser.firstName || "",
|
|
9
|
-
lastName: apiUser.last_name || apiUser.lastName || "",
|
|
10
|
-
avatar: apiUser.avatar_url || apiUser.avatar,
|
|
11
|
-
mfaEnabled: apiUser.mfa_enabled ?? apiUser.mfaEnabled ?? false,
|
|
12
|
-
emailVerified: apiUser.email_verified ?? apiUser.emailVerified ?? false,
|
|
13
|
-
passwordUpdatedAt: apiUser.password_updated_at || apiUser.passwordUpdatedAt,
|
|
14
|
-
settings: apiUser.settings || {},
|
|
15
|
-
xp: apiUser.xp || 0,
|
|
16
|
-
level: apiUser.level || 1,
|
|
17
|
-
next_level_xp: apiUser.next_level_xp || apiUser.next_level_xp || 4,
|
|
18
|
-
username: apiUser.username,
|
|
19
|
-
usernameLastChangedAt: apiUser.username_last_changed_at || apiUser.usernameLastChangedAt,
|
|
20
|
-
createdAt: apiUser.created_at || apiUser.createdAt,
|
|
21
|
-
updatedAt: apiUser.updated_at || apiUser.updatedAt || (/* @__PURE__ */ new Date()).toISOString()
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
const { getUrl } = useStrandsConfig.useStrandsConfig();
|
|
25
|
-
const currentUser = vue.ref(null);
|
|
26
|
-
const currentSession = vue.ref(null);
|
|
27
|
-
const isInitializing = vue.ref(true);
|
|
28
|
-
const isSigningIn = vue.ref(false);
|
|
29
|
-
const isSigningUp = vue.ref(false);
|
|
30
|
-
const isSigningOut = vue.ref(false);
|
|
31
|
-
const isRefreshingToken = vue.ref(false);
|
|
32
|
-
const isSendingMfaEmail = vue.ref(false);
|
|
33
|
-
const isVerifyingMfa = vue.ref(false);
|
|
34
|
-
const isLoadingProfile = vue.ref(false);
|
|
35
|
-
const loading = vue.computed(() => isSigningIn.value || isSigningUp.value || isSigningOut.value || isRefreshingToken.value || isSendingMfaEmail.value || isVerifyingMfa.value || isLoadingProfile.value);
|
|
36
|
-
const isLoading = vue.computed(() => isInitializing.value || loading.value);
|
|
37
|
-
const loadingMessage = vue.computed(() => {
|
|
38
|
-
if (isInitializing.value) return "Checking authentication...";
|
|
39
|
-
if (isSigningIn.value) return "Signing you in...";
|
|
40
|
-
if (isSigningUp.value) return "Creating your account...";
|
|
41
|
-
if (isSigningOut.value) return "Signing you out...";
|
|
42
|
-
if (isRefreshingToken.value) return "Refreshing session...";
|
|
43
|
-
if (isSendingMfaEmail.value) return "Sending verification code...";
|
|
44
|
-
if (isVerifyingMfa.value) return "Verifying code...";
|
|
45
|
-
return "Loading...";
|
|
46
|
-
});
|
|
47
|
-
const isInitialized = vue.ref(false);
|
|
48
|
-
const mfaRequired = vue.ref(false);
|
|
49
|
-
const mfaSessionId = vue.ref(null);
|
|
50
|
-
const availableMfaMethods = vue.ref([]);
|
|
51
|
-
let refreshTimer = null;
|
|
52
|
-
function useStrandsAuth() {
|
|
53
|
-
const getAuthHeaders = () => {
|
|
54
|
-
if (!currentSession.value?.accessToken) {
|
|
55
|
-
throw new Error("No access token available");
|
|
56
|
-
}
|
|
57
|
-
const headers = {
|
|
58
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
59
|
-
};
|
|
60
|
-
if (currentSession.value?.refreshToken) {
|
|
61
|
-
headers["x-refresh-token"] = currentSession.value.refreshToken;
|
|
62
|
-
}
|
|
63
|
-
return headers;
|
|
64
|
-
};
|
|
65
|
-
const completeHardwareKeyRegistration = async (deviceId, credential, accessToken) => {
|
|
66
|
-
if (!currentSession.value?.accessToken) {
|
|
67
|
-
throw new Error("No access token available");
|
|
68
|
-
}
|
|
69
|
-
const response = await fetch(getUrl("mfaHardwareCompleteRegistration"), {
|
|
70
|
-
method: "POST",
|
|
71
|
-
headers: {
|
|
72
|
-
"Content-Type": "application/json",
|
|
73
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
74
|
-
},
|
|
75
|
-
body: JSON.stringify({
|
|
76
|
-
device_id: deviceId,
|
|
77
|
-
credential
|
|
78
|
-
})
|
|
79
|
-
});
|
|
80
|
-
if (!response.ok) {
|
|
81
|
-
const errorText = await response.text();
|
|
82
|
-
let errorMessage = "Failed to complete hardware key registration";
|
|
83
|
-
try {
|
|
84
|
-
const errorData = JSON.parse(errorText);
|
|
85
|
-
errorMessage = errorData.message || errorData.error || errorText;
|
|
86
|
-
} catch {
|
|
87
|
-
errorMessage = errorText || "Failed to complete hardware key registration";
|
|
88
|
-
}
|
|
89
|
-
throw new Error(errorMessage);
|
|
90
|
-
}
|
|
91
|
-
return response.json();
|
|
92
|
-
};
|
|
93
|
-
const registerHardwareKey = async (deviceName, accessToken, deviceType = "hardware") => {
|
|
94
|
-
if (!currentSession.value?.accessToken) {
|
|
95
|
-
throw new Error("No access token available");
|
|
96
|
-
}
|
|
97
|
-
const response = await fetch(getUrl("mfaHardwareStartRegistration"), {
|
|
98
|
-
method: "POST",
|
|
99
|
-
headers: {
|
|
100
|
-
"Content-Type": "application/json",
|
|
101
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
102
|
-
},
|
|
103
|
-
body: JSON.stringify({
|
|
104
|
-
device_name: deviceName,
|
|
105
|
-
device_type: deviceType
|
|
106
|
-
})
|
|
107
|
-
});
|
|
108
|
-
if (!response.ok) {
|
|
109
|
-
const errorText = await response.text();
|
|
110
|
-
let errorMessage = "Failed to start hardware key registration";
|
|
111
|
-
try {
|
|
112
|
-
const errorData = JSON.parse(errorText);
|
|
113
|
-
errorMessage = errorData.message || errorData.error || errorText;
|
|
114
|
-
} catch {
|
|
115
|
-
errorMessage = errorText || "Failed to start hardware key registration";
|
|
116
|
-
}
|
|
117
|
-
throw new Error(errorMessage);
|
|
118
|
-
}
|
|
119
|
-
return response.json();
|
|
120
|
-
};
|
|
121
|
-
const isAuthenticated = vue.computed(() => currentUser.value !== null);
|
|
122
|
-
const signIn = async (credentials) => {
|
|
123
|
-
isSigningIn.value = true;
|
|
124
|
-
try {
|
|
125
|
-
mfaRequired.value = false;
|
|
126
|
-
mfaSessionId.value = null;
|
|
127
|
-
availableMfaMethods.value = [];
|
|
128
|
-
const response = await fetch(getUrl("signIn"), {
|
|
129
|
-
method: "POST",
|
|
130
|
-
headers: {
|
|
131
|
-
"Content-Type": "application/json"
|
|
132
|
-
},
|
|
133
|
-
body: JSON.stringify(credentials)
|
|
134
|
-
});
|
|
135
|
-
if (!response.ok) {
|
|
136
|
-
if (response.status === 401) {
|
|
137
|
-
throw new Error("Invalid email or password");
|
|
138
|
-
} else if (response.status === 403) {
|
|
139
|
-
throw new Error("Please verify your email address before signing in");
|
|
140
|
-
} else {
|
|
141
|
-
throw new Error(`Sign in failed: ${response.status} ${response.statusText}`);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
const authData = await response.json();
|
|
145
|
-
if (authData.mfa_required) {
|
|
146
|
-
mfaRequired.value = true;
|
|
147
|
-
mfaSessionId.value = authData.mfa_session_id || null;
|
|
148
|
-
const methods = (authData.available_mfa_methods || []).map((method) => {
|
|
149
|
-
let fallbackName = `${method.device_type.charAt(0).toUpperCase() + method.device_type.slice(1)} Authentication`;
|
|
150
|
-
if (method.device_type === "hardware") {
|
|
151
|
-
fallbackName = method.device_name || "Security Key";
|
|
152
|
-
} else if (method.device_type === "totp") {
|
|
153
|
-
fallbackName = method.device_name || "Authenticator App";
|
|
154
|
-
} else if (method.device_type === "email") {
|
|
155
|
-
fallbackName = method.device_name || "Email Verification";
|
|
156
|
-
}
|
|
157
|
-
return {
|
|
158
|
-
id: method.device_id,
|
|
159
|
-
device_type: method.device_type,
|
|
160
|
-
device_name: method.device_name || fallbackName,
|
|
161
|
-
is_active: true,
|
|
162
|
-
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
163
|
-
last_used_at: method.last_used_at,
|
|
164
|
-
// Pass through additional metadata if available
|
|
165
|
-
credential_id: method.credential_id,
|
|
166
|
-
device_info: method.device_info
|
|
167
|
-
};
|
|
168
|
-
});
|
|
169
|
-
availableMfaMethods.value = methods;
|
|
170
|
-
isSigningIn.value = false;
|
|
171
|
-
return authData;
|
|
172
|
-
}
|
|
173
|
-
await setAuthData(authData);
|
|
174
|
-
return authData;
|
|
175
|
-
} catch (error) {
|
|
176
|
-
throw error;
|
|
177
|
-
} finally {
|
|
178
|
-
isSigningIn.value = false;
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
const signUp = async (userData) => {
|
|
182
|
-
isSigningUp.value = true;
|
|
183
|
-
try {
|
|
184
|
-
throw new Error("Sign up not implemented - please integrate with auth SDK");
|
|
185
|
-
} finally {
|
|
186
|
-
isSigningUp.value = false;
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
const signOut = async () => {
|
|
190
|
-
isSigningOut.value = true;
|
|
191
|
-
try {
|
|
192
|
-
stopTokenRefreshTimer();
|
|
193
|
-
currentUser.value = null;
|
|
194
|
-
currentSession.value = null;
|
|
195
|
-
mfaRequired.value = false;
|
|
196
|
-
mfaSessionId.value = null;
|
|
197
|
-
availableMfaMethods.value = [];
|
|
198
|
-
if (typeof window !== "undefined") {
|
|
199
|
-
localStorage.removeItem("strands_auth_session");
|
|
200
|
-
localStorage.removeItem("strands_auth_user");
|
|
201
|
-
}
|
|
202
|
-
} finally {
|
|
203
|
-
isSigningOut.value = false;
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
const refreshToken = async () => {
|
|
207
|
-
if (!currentSession.value?.refreshToken) {
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
isRefreshingToken.value = true;
|
|
211
|
-
try {
|
|
212
|
-
const response = await fetch(getUrl("refresh"), {
|
|
213
|
-
method: "POST",
|
|
214
|
-
headers: {
|
|
215
|
-
"Content-Type": "application/json"
|
|
216
|
-
},
|
|
217
|
-
body: JSON.stringify({
|
|
218
|
-
refresh_token: currentSession.value.refreshToken
|
|
219
|
-
})
|
|
220
|
-
});
|
|
221
|
-
if (!response.ok) {
|
|
222
|
-
if (response.status === 401) {
|
|
223
|
-
await signOut();
|
|
224
|
-
return false;
|
|
225
|
-
}
|
|
226
|
-
throw new Error(`Token refresh failed: ${response.status} ${response.statusText}`);
|
|
227
|
-
}
|
|
228
|
-
const tokenData = await response.json();
|
|
229
|
-
if (tokenData.user) {
|
|
230
|
-
currentUser.value = mapApiUserToFrontend(tokenData.user);
|
|
231
|
-
if (typeof window !== "undefined") {
|
|
232
|
-
localStorage.setItem("strands_auth_user", JSON.stringify(currentUser.value));
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
const newSession = {
|
|
236
|
-
accessToken: tokenData.access_token,
|
|
237
|
-
refreshToken: tokenData.refresh_token,
|
|
238
|
-
expiresAt: new Date(Date.now() + 5 * 60 * 1e3),
|
|
239
|
-
// 5 minutes from now
|
|
240
|
-
userId: tokenData.user?.id || currentUser.value?.id
|
|
241
|
-
};
|
|
242
|
-
currentSession.value = newSession;
|
|
243
|
-
if (typeof window !== "undefined") {
|
|
244
|
-
localStorage.setItem("strands_auth_session", JSON.stringify(newSession));
|
|
245
|
-
}
|
|
246
|
-
startTokenRefreshTimer();
|
|
247
|
-
return true;
|
|
248
|
-
} catch (error) {
|
|
249
|
-
await signOut();
|
|
250
|
-
return false;
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
const fetchProfile = async () => {
|
|
254
|
-
if (!currentSession.value?.accessToken) {
|
|
255
|
-
throw new Error("No access token available");
|
|
256
|
-
}
|
|
257
|
-
isLoadingProfile.value = true;
|
|
258
|
-
try {
|
|
259
|
-
const response = await fetch(getUrl("profile"), {
|
|
260
|
-
method: "GET",
|
|
261
|
-
headers: {
|
|
262
|
-
"Content-Type": "application/json",
|
|
263
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
if (!response.ok) {
|
|
267
|
-
if (response.status === 401) {
|
|
268
|
-
throw new Error("Authentication expired. Please sign in again.");
|
|
269
|
-
} else {
|
|
270
|
-
throw new Error(`Failed to fetch profile: ${response.status} ${response.statusText}`);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
const userData = await response.json();
|
|
274
|
-
currentUser.value = mapApiUserToFrontend(userData);
|
|
275
|
-
if (typeof window !== "undefined") {
|
|
276
|
-
localStorage.setItem("strands_auth_user", JSON.stringify(currentUser.value));
|
|
277
|
-
}
|
|
278
|
-
return currentUser.value;
|
|
279
|
-
} finally {
|
|
280
|
-
isLoadingProfile.value = false;
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
const updateProfile = async (profileData) => {
|
|
284
|
-
if (!currentSession.value?.accessToken) {
|
|
285
|
-
throw new Error("No access token available");
|
|
286
|
-
}
|
|
287
|
-
isLoadingProfile.value = true;
|
|
288
|
-
try {
|
|
289
|
-
const response = await fetch(getUrl("profile"), {
|
|
290
|
-
method: "POST",
|
|
291
|
-
headers: {
|
|
292
|
-
"Content-Type": "application/json",
|
|
293
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
294
|
-
},
|
|
295
|
-
body: JSON.stringify({
|
|
296
|
-
first_name: profileData.firstName,
|
|
297
|
-
last_name: profileData.lastName
|
|
298
|
-
})
|
|
299
|
-
});
|
|
300
|
-
if (!response.ok) {
|
|
301
|
-
if (response.status === 401) {
|
|
302
|
-
throw new Error("Authentication expired. Please sign in again.");
|
|
303
|
-
} else {
|
|
304
|
-
throw new Error(`Profile update failed: ${response.status} ${response.statusText}`);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
const updatedUserData = await response.json();
|
|
308
|
-
currentUser.value = mapApiUserToFrontend(updatedUserData);
|
|
309
|
-
if (typeof window !== "undefined") {
|
|
310
|
-
localStorage.setItem("strands_auth_user", JSON.stringify(currentUser.value));
|
|
311
|
-
}
|
|
312
|
-
return currentUser.value;
|
|
313
|
-
} finally {
|
|
314
|
-
isLoadingProfile.value = false;
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
const updateUserSettings = async (settings) => {
|
|
318
|
-
if (!currentSession.value?.accessToken) {
|
|
319
|
-
throw new Error("No access token available");
|
|
320
|
-
}
|
|
321
|
-
isLoadingProfile.value = true;
|
|
322
|
-
try {
|
|
323
|
-
const response = await fetch(getUrl("settings"), {
|
|
324
|
-
method: "POST",
|
|
325
|
-
headers: {
|
|
326
|
-
"Content-Type": "application/json",
|
|
327
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
328
|
-
},
|
|
329
|
-
body: JSON.stringify({
|
|
330
|
-
settings
|
|
331
|
-
})
|
|
332
|
-
});
|
|
333
|
-
if (!response.ok) {
|
|
334
|
-
if (response.status === 401) {
|
|
335
|
-
throw new Error("Authentication expired. Please sign in again.");
|
|
336
|
-
} else {
|
|
337
|
-
throw new Error(`Settings update failed: ${response.status} ${response.statusText}`);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
const updatedUserData = await response.json();
|
|
341
|
-
currentUser.value = mapApiUserToFrontend(updatedUserData);
|
|
342
|
-
if (typeof window !== "undefined") {
|
|
343
|
-
localStorage.setItem("strands_auth_user", JSON.stringify(currentUser.value));
|
|
344
|
-
}
|
|
345
|
-
return currentUser.value;
|
|
346
|
-
} finally {
|
|
347
|
-
isLoadingProfile.value = false;
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
const changeEmail = async (newEmail, password) => {
|
|
351
|
-
if (!currentSession.value?.accessToken) {
|
|
352
|
-
throw new Error("No access token available");
|
|
353
|
-
}
|
|
354
|
-
isLoadingProfile.value = true;
|
|
355
|
-
try {
|
|
356
|
-
const response = await fetch(getUrl("changeEmail"), {
|
|
357
|
-
method: "POST",
|
|
358
|
-
headers: {
|
|
359
|
-
"Content-Type": "application/json",
|
|
360
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
361
|
-
},
|
|
362
|
-
body: JSON.stringify({
|
|
363
|
-
new_email: newEmail,
|
|
364
|
-
password
|
|
365
|
-
})
|
|
366
|
-
});
|
|
367
|
-
if (!response.ok) {
|
|
368
|
-
if (response.status === 401) {
|
|
369
|
-
throw new Error("Authentication expired. Please sign in again.");
|
|
370
|
-
} else {
|
|
371
|
-
const errorData = await response.json().catch(() => ({}));
|
|
372
|
-
throw new Error(errorData.message || `Email change failed: ${response.status} ${response.statusText}`);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
const result = await response.json();
|
|
376
|
-
if (currentUser.value) {
|
|
377
|
-
currentUser.value = {
|
|
378
|
-
...currentUser.value,
|
|
379
|
-
email: newEmail,
|
|
380
|
-
emailVerified: false,
|
|
381
|
-
// Email needs to be verified again
|
|
382
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
383
|
-
};
|
|
384
|
-
if (typeof window !== "undefined") {
|
|
385
|
-
localStorage.setItem("strands_auth_user", JSON.stringify(currentUser.value));
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
return result;
|
|
389
|
-
} finally {
|
|
390
|
-
isLoadingProfile.value = false;
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
const verifyMfa = async (deviceId, code, isBackupCode = false) => {
|
|
394
|
-
if (!mfaSessionId.value) {
|
|
395
|
-
throw new Error("No MFA session available");
|
|
396
|
-
}
|
|
397
|
-
isVerifyingMfa.value = true;
|
|
398
|
-
try {
|
|
399
|
-
const endpoint = isBackupCode ? getUrl("mfaBackupCodeVerify") : getUrl("mfaSigninVerify");
|
|
400
|
-
const body = isBackupCode ? { mfa_session_id: mfaSessionId.value, backup_code: code } : { mfa_session_id: mfaSessionId.value, device_id: deviceId, code };
|
|
401
|
-
const response = await fetch(endpoint, {
|
|
402
|
-
method: "POST",
|
|
403
|
-
headers: { "Content-Type": "application/json" },
|
|
404
|
-
body: JSON.stringify(body)
|
|
405
|
-
});
|
|
406
|
-
if (!response.ok) {
|
|
407
|
-
const errorText = await response.text();
|
|
408
|
-
let errorMessage = "MFA verification failed";
|
|
409
|
-
try {
|
|
410
|
-
const errorData = JSON.parse(errorText);
|
|
411
|
-
errorMessage = errorData.message || errorData.error || errorText;
|
|
412
|
-
} catch {
|
|
413
|
-
errorMessage = errorText || "MFA verification failed";
|
|
414
|
-
}
|
|
415
|
-
throw new Error(errorMessage);
|
|
416
|
-
}
|
|
417
|
-
const authData = await response.json();
|
|
418
|
-
mfaRequired.value = false;
|
|
419
|
-
mfaSessionId.value = null;
|
|
420
|
-
availableMfaMethods.value = [];
|
|
421
|
-
await setAuthData(authData);
|
|
422
|
-
return authData;
|
|
423
|
-
} finally {
|
|
424
|
-
isVerifyingMfa.value = false;
|
|
425
|
-
}
|
|
426
|
-
};
|
|
427
|
-
const sendMfaEmailCode = async (deviceId) => {
|
|
428
|
-
if (!mfaSessionId.value) {
|
|
429
|
-
throw new Error("No MFA session available");
|
|
430
|
-
}
|
|
431
|
-
isSendingMfaEmail.value = true;
|
|
432
|
-
try {
|
|
433
|
-
const response = await fetch(getUrl("mfaSigninSendEmail"), {
|
|
434
|
-
method: "POST",
|
|
435
|
-
headers: { "Content-Type": "application/json" },
|
|
436
|
-
body: JSON.stringify({
|
|
437
|
-
mfa_session_id: mfaSessionId.value,
|
|
438
|
-
device_id: deviceId
|
|
439
|
-
})
|
|
440
|
-
});
|
|
441
|
-
if (!response.ok) {
|
|
442
|
-
const errorText = await response.text();
|
|
443
|
-
let errorMessage = "Failed to send MFA email code";
|
|
444
|
-
try {
|
|
445
|
-
const errorData = JSON.parse(errorText);
|
|
446
|
-
errorMessage = errorData.message || errorData.error || errorText;
|
|
447
|
-
} catch {
|
|
448
|
-
errorMessage = errorText || "Failed to send MFA email code";
|
|
449
|
-
}
|
|
450
|
-
throw new Error(errorMessage);
|
|
451
|
-
}
|
|
452
|
-
const result = await response.json();
|
|
453
|
-
return result;
|
|
454
|
-
} finally {
|
|
455
|
-
isSendingMfaEmail.value = false;
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
const getMfaWebAuthnChallenge = async (deviceId) => {
|
|
459
|
-
if (!mfaSessionId.value) {
|
|
460
|
-
throw new Error("No MFA session available");
|
|
461
|
-
}
|
|
462
|
-
const response = await fetch(getUrl("mfaWebAuthnChallenge"), {
|
|
463
|
-
method: "POST",
|
|
464
|
-
headers: { "Content-Type": "application/json" },
|
|
465
|
-
body: JSON.stringify({
|
|
466
|
-
mfa_session_id: mfaSessionId.value,
|
|
467
|
-
device_id: deviceId
|
|
468
|
-
})
|
|
469
|
-
});
|
|
470
|
-
if (!response.ok) {
|
|
471
|
-
const errorText = await response.text();
|
|
472
|
-
let errorMessage = "Failed to get WebAuthn challenge";
|
|
473
|
-
try {
|
|
474
|
-
const errorData = JSON.parse(errorText);
|
|
475
|
-
errorMessage = errorData.message || errorData.error || errorText;
|
|
476
|
-
} catch {
|
|
477
|
-
errorMessage = errorText || errorMessage;
|
|
478
|
-
}
|
|
479
|
-
throw new Error(errorMessage);
|
|
480
|
-
}
|
|
481
|
-
return response.json();
|
|
482
|
-
};
|
|
483
|
-
const setAuthData = async (authResponse) => {
|
|
484
|
-
try {
|
|
485
|
-
if (authResponse.user) {
|
|
486
|
-
currentUser.value = mapApiUserToFrontend(authResponse.user);
|
|
487
|
-
}
|
|
488
|
-
const session = {
|
|
489
|
-
accessToken: authResponse.access_token,
|
|
490
|
-
refreshToken: authResponse.refresh_token,
|
|
491
|
-
expiresAt: new Date(Date.now() + 5 * 60 * 1e3),
|
|
492
|
-
// 5 minutes from now (matching API token expiry)
|
|
493
|
-
userId: currentUser.value?.id || authResponse.user?.id
|
|
494
|
-
};
|
|
495
|
-
currentSession.value = session;
|
|
496
|
-
if (typeof window !== "undefined") {
|
|
497
|
-
localStorage.setItem("strands_auth_session", JSON.stringify(session));
|
|
498
|
-
if (currentUser.value) {
|
|
499
|
-
localStorage.setItem("strands_auth_user", JSON.stringify(currentUser.value));
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
startTokenRefreshTimer();
|
|
503
|
-
} catch (error) {
|
|
504
|
-
console.error("Error setting auth data:", error);
|
|
505
|
-
}
|
|
506
|
-
};
|
|
507
|
-
const startTokenRefreshTimer = () => {
|
|
508
|
-
if (refreshTimer) {
|
|
509
|
-
clearTimeout(refreshTimer);
|
|
510
|
-
}
|
|
511
|
-
if (!currentSession.value) return;
|
|
512
|
-
const now = /* @__PURE__ */ new Date();
|
|
513
|
-
const expiresAt = currentSession.value.expiresAt;
|
|
514
|
-
const timeUntilRefresh = expiresAt.getTime() - now.getTime() - 1 * 60 * 1e3;
|
|
515
|
-
if (timeUntilRefresh <= 0) {
|
|
516
|
-
refreshToken();
|
|
517
|
-
return;
|
|
518
|
-
}
|
|
519
|
-
refreshTimer = setTimeout(async () => {
|
|
520
|
-
const success = await refreshToken();
|
|
521
|
-
if (success) {
|
|
522
|
-
startTokenRefreshTimer();
|
|
523
|
-
}
|
|
524
|
-
}, timeUntilRefresh);
|
|
525
|
-
};
|
|
526
|
-
const stopTokenRefreshTimer = () => {
|
|
527
|
-
if (refreshTimer) {
|
|
528
|
-
clearTimeout(refreshTimer);
|
|
529
|
-
refreshTimer = null;
|
|
530
|
-
}
|
|
531
|
-
};
|
|
532
|
-
const initialize = async () => {
|
|
533
|
-
if (isInitialized.value) return;
|
|
534
|
-
isInitializing.value = true;
|
|
535
|
-
try {
|
|
536
|
-
if (typeof window !== "undefined") {
|
|
537
|
-
const storedSession = localStorage.getItem("strands_auth_session");
|
|
538
|
-
const storedUser = localStorage.getItem("strands_auth_user");
|
|
539
|
-
if (storedSession && storedUser) {
|
|
540
|
-
try {
|
|
541
|
-
const session = JSON.parse(storedSession);
|
|
542
|
-
const user = JSON.parse(storedUser);
|
|
543
|
-
session.expiresAt = new Date(session.expiresAt);
|
|
544
|
-
if (session.expiresAt > /* @__PURE__ */ new Date()) {
|
|
545
|
-
currentSession.value = session;
|
|
546
|
-
currentUser.value = user;
|
|
547
|
-
startTokenRefreshTimer();
|
|
548
|
-
} else {
|
|
549
|
-
localStorage.removeItem("strands_auth_session");
|
|
550
|
-
localStorage.removeItem("strands_auth_user");
|
|
551
|
-
}
|
|
552
|
-
} catch (error) {
|
|
553
|
-
localStorage.removeItem("strands_auth_session");
|
|
554
|
-
localStorage.removeItem("strands_auth_user");
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
isInitialized.value = true;
|
|
559
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
560
|
-
} catch (error) {
|
|
561
|
-
} finally {
|
|
562
|
-
isInitializing.value = false;
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
|
-
const changeUsername = async (newUsername) => {
|
|
566
|
-
if (!currentSession.value?.accessToken) {
|
|
567
|
-
throw new Error("No access token available");
|
|
568
|
-
}
|
|
569
|
-
isLoadingProfile.value = true;
|
|
570
|
-
try {
|
|
571
|
-
const response = await fetch(getUrl("changeUsername"), {
|
|
572
|
-
method: "POST",
|
|
573
|
-
headers: {
|
|
574
|
-
"Content-Type": "application/json",
|
|
575
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
576
|
-
},
|
|
577
|
-
body: JSON.stringify({
|
|
578
|
-
username: newUsername
|
|
579
|
-
})
|
|
580
|
-
});
|
|
581
|
-
if (!response.ok) {
|
|
582
|
-
const errorData = await response.json().catch(() => ({}));
|
|
583
|
-
if (response.status === 409) {
|
|
584
|
-
throw new Error("Username is already taken");
|
|
585
|
-
} else if (errorData.cooldown_end) {
|
|
586
|
-
throw new Error(`You can only change your username once every 30 days. You can change it again on ${new Date(errorData.cooldown_end).toLocaleDateString()}`);
|
|
587
|
-
}
|
|
588
|
-
throw new Error(errorData.message || `Username change failed: ${response.status} ${response.statusText}`);
|
|
589
|
-
}
|
|
590
|
-
const result = await response.json();
|
|
591
|
-
if (currentUser.value) {
|
|
592
|
-
currentUser.value = {
|
|
593
|
-
...currentUser.value,
|
|
594
|
-
username: newUsername,
|
|
595
|
-
usernameLastChangedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
596
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
597
|
-
};
|
|
598
|
-
if (typeof window !== "undefined") {
|
|
599
|
-
localStorage.setItem("strands_auth_user", JSON.stringify(currentUser.value));
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
return result;
|
|
603
|
-
} finally {
|
|
604
|
-
isLoadingProfile.value = false;
|
|
605
|
-
}
|
|
606
|
-
};
|
|
607
|
-
const getUsernameCooldown = async () => {
|
|
608
|
-
if (!currentSession.value?.accessToken) {
|
|
609
|
-
throw new Error("No access token available");
|
|
610
|
-
}
|
|
611
|
-
const response = await fetch(getUrl("usernameCooldown"), {
|
|
612
|
-
method: "GET",
|
|
613
|
-
headers: {
|
|
614
|
-
"Authorization": `Bearer ${currentSession.value.accessToken}`
|
|
615
|
-
}
|
|
616
|
-
});
|
|
617
|
-
if (!response.ok) {
|
|
618
|
-
throw new Error(`Failed to get username cooldown: ${response.status} ${response.statusText}`);
|
|
619
|
-
}
|
|
620
|
-
return response.json();
|
|
621
|
-
};
|
|
622
|
-
const checkUsernameAvailability = async (username) => {
|
|
623
|
-
const url = getUrl("checkUsernameAvailability").replace("{username}", encodeURIComponent(username));
|
|
624
|
-
const response = await fetch(url, {
|
|
625
|
-
method: "GET",
|
|
626
|
-
headers: {
|
|
627
|
-
"Content-Type": "application/json"
|
|
628
|
-
}
|
|
629
|
-
});
|
|
630
|
-
if (!response.ok) {
|
|
631
|
-
throw new Error(`Failed to check username availability: ${response.status} ${response.statusText}`);
|
|
632
|
-
}
|
|
633
|
-
return response.json();
|
|
634
|
-
};
|
|
635
|
-
const getUserSessions = async () => {
|
|
636
|
-
const response = await fetch(getUrl("sessions"), {
|
|
637
|
-
method: "GET",
|
|
638
|
-
headers: getAuthHeaders()
|
|
639
|
-
});
|
|
640
|
-
if (!response.ok) {
|
|
641
|
-
throw new Error(`Failed to get user sessions: ${response.status} ${response.statusText}`);
|
|
642
|
-
}
|
|
643
|
-
return response.json();
|
|
644
|
-
};
|
|
645
|
-
const getSessionStats = async () => {
|
|
646
|
-
const response = await fetch(getUrl("sessionsStats"), {
|
|
647
|
-
method: "GET",
|
|
648
|
-
headers: getAuthHeaders()
|
|
649
|
-
});
|
|
650
|
-
if (!response.ok) {
|
|
651
|
-
throw new Error(`Failed to get session stats: ${response.status} ${response.statusText}`);
|
|
652
|
-
}
|
|
653
|
-
return response.json();
|
|
654
|
-
};
|
|
655
|
-
const revokeSession = async (sessionId) => {
|
|
656
|
-
const url = getUrl("sessionRevoke").replace("{session_id}", encodeURIComponent(sessionId));
|
|
657
|
-
const response = await fetch(url, {
|
|
658
|
-
method: "POST",
|
|
659
|
-
headers: getAuthHeaders()
|
|
660
|
-
});
|
|
661
|
-
if (!response.ok) {
|
|
662
|
-
throw new Error(`Failed to revoke session: ${response.status} ${response.statusText}`);
|
|
663
|
-
}
|
|
664
|
-
return response.status === 200;
|
|
665
|
-
};
|
|
666
|
-
const revokeAllOtherSessions = async () => {
|
|
667
|
-
const response = await fetch(getUrl("sessionsRevokeAll"), {
|
|
668
|
-
method: "POST",
|
|
669
|
-
headers: getAuthHeaders()
|
|
670
|
-
});
|
|
671
|
-
if (!response.ok) {
|
|
672
|
-
throw new Error(`Failed to revoke all other sessions: ${response.status} ${response.statusText}`);
|
|
673
|
-
}
|
|
674
|
-
return response.status === 200;
|
|
675
|
-
};
|
|
676
|
-
if (!isInitialized.value) {
|
|
677
|
-
initialize();
|
|
678
|
-
}
|
|
679
|
-
return {
|
|
680
|
-
// State
|
|
681
|
-
user: vue.computed(() => currentUser.value),
|
|
682
|
-
currentUser: vue.computed(() => currentUser.value),
|
|
683
|
-
currentSession: vue.computed(() => currentSession.value),
|
|
684
|
-
isAuthenticated,
|
|
685
|
-
isLoading: vue.computed(() => isLoading.value || !isInitialized.value),
|
|
686
|
-
loading: vue.computed(() => loading.value),
|
|
687
|
-
loadingMessage,
|
|
688
|
-
// Specific loading states
|
|
689
|
-
isInitializing,
|
|
690
|
-
isSigningIn,
|
|
691
|
-
isSigningUp,
|
|
692
|
-
isSigningOut,
|
|
693
|
-
isRefreshingToken,
|
|
694
|
-
isSendingMfaEmail,
|
|
695
|
-
isVerifyingMfa,
|
|
696
|
-
// MFA State
|
|
697
|
-
mfaRequired: vue.computed(() => mfaRequired.value),
|
|
698
|
-
mfaSessionId: vue.computed(() => mfaSessionId.value),
|
|
699
|
-
availableMfaMethods: vue.computed(() => availableMfaMethods.value),
|
|
700
|
-
// Methods
|
|
701
|
-
signIn,
|
|
702
|
-
signUp,
|
|
703
|
-
signOut,
|
|
704
|
-
refreshToken,
|
|
705
|
-
fetchProfile,
|
|
706
|
-
updateProfile,
|
|
707
|
-
updateUserSettings,
|
|
708
|
-
changeEmail,
|
|
709
|
-
changeUsername,
|
|
710
|
-
getUsernameCooldown,
|
|
711
|
-
checkUsernameAvailability,
|
|
712
|
-
// Session management
|
|
713
|
-
getUserSessions,
|
|
714
|
-
getSessionStats,
|
|
715
|
-
revokeSession,
|
|
716
|
-
revokeAllOtherSessions,
|
|
717
|
-
initialize,
|
|
718
|
-
setAuthData,
|
|
719
|
-
verifyMfa,
|
|
720
|
-
sendMfaEmailCode,
|
|
721
|
-
getMfaWebAuthnChallenge,
|
|
722
|
-
registerHardwareKey,
|
|
723
|
-
completeHardwareKeyRegistration,
|
|
724
|
-
// Token management
|
|
725
|
-
startTokenRefreshTimer,
|
|
726
|
-
stopTokenRefreshTimer,
|
|
727
|
-
// Force re-initialization (useful for testing or navigation)
|
|
728
|
-
forceReInit: () => {
|
|
729
|
-
isInitialized.value = false;
|
|
730
|
-
isInitializing.value = true;
|
|
731
|
-
initialize();
|
|
732
|
-
}
|
|
733
|
-
};
|
|
734
|
-
}
|
|
735
|
-
exports.useStrandsAuth = useStrandsAuth;
|
|
736
|
-
//# sourceMappingURL=useStrandsAuth-Co2lLH4X.cjs.map
|