@lvce-editor/auth-worker 1.16.0 → 1.17.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/dist/authWorkerMain.js +27 -19
- package/package.json +1 -1
package/dist/authWorkerMain.js
CHANGED
|
@@ -1184,14 +1184,21 @@ const getString = (value, fallback = '') => {
|
|
|
1184
1184
|
return typeof value === 'string' ? value : fallback;
|
|
1185
1185
|
};
|
|
1186
1186
|
|
|
1187
|
+
const getUserName = value => {
|
|
1188
|
+
if (typeof value.userName === 'string') {
|
|
1189
|
+
return value.userName;
|
|
1190
|
+
}
|
|
1191
|
+
return getString(value.user?.displayName);
|
|
1192
|
+
};
|
|
1187
1193
|
const toBackendAuthState = value => {
|
|
1188
1194
|
return {
|
|
1189
1195
|
authAccessToken: getString(value.accessToken),
|
|
1190
1196
|
authErrorMessage: getString(value.error),
|
|
1191
1197
|
authRefreshToken: getString(value.refreshToken),
|
|
1192
|
-
userName:
|
|
1198
|
+
userName: getUserName(value),
|
|
1193
1199
|
userState: value.accessToken ? 'loggedIn' : 'loggedOut',
|
|
1194
1200
|
userSubscriptionPlan: getString(value.subscriptionPlan),
|
|
1201
|
+
userSubscriptionStatus: getString(value.subscriptionStatus),
|
|
1195
1202
|
userUsedTokens: getNumber(value.usedTokens)
|
|
1196
1203
|
};
|
|
1197
1204
|
};
|
|
@@ -1203,6 +1210,13 @@ const parseBackendAuthResponse = value => {
|
|
|
1203
1210
|
return toBackendAuthState(value);
|
|
1204
1211
|
};
|
|
1205
1212
|
|
|
1213
|
+
const getPayload = async response => {
|
|
1214
|
+
try {
|
|
1215
|
+
return await response.json();
|
|
1216
|
+
} catch {
|
|
1217
|
+
return undefined;
|
|
1218
|
+
}
|
|
1219
|
+
};
|
|
1206
1220
|
const syncBackendAuth = async backendUrl => {
|
|
1207
1221
|
if (!backendUrl) {
|
|
1208
1222
|
return getLoggedOutBackendAuthState('Backend URL is missing.');
|
|
@@ -1222,12 +1236,7 @@ const syncBackendAuth = async backendUrl => {
|
|
|
1222
1236
|
if (response.status === 401 || response.status === 403) {
|
|
1223
1237
|
return getLoggedOutBackendAuthState();
|
|
1224
1238
|
}
|
|
1225
|
-
|
|
1226
|
-
try {
|
|
1227
|
-
payload = await response.json();
|
|
1228
|
-
} catch {
|
|
1229
|
-
payload = undefined;
|
|
1230
|
-
}
|
|
1239
|
+
const payload = await getPayload(response);
|
|
1231
1240
|
if (!response.ok) {
|
|
1232
1241
|
const parsed = parseBackendAuthResponse(payload);
|
|
1233
1242
|
return getLoggedOutBackendAuthState(parsed.authErrorMessage || 'Backend authentication failed.');
|
|
@@ -2380,15 +2389,13 @@ const getLoggedInState = (state, response) => {
|
|
|
2380
2389
|
userName: typeof response.userName === 'string' ? response.userName : state.userName,
|
|
2381
2390
|
userState: accessToken ? 'loggedIn' : 'loggedOut',
|
|
2382
2391
|
userSubscriptionPlan: typeof response.subscriptionPlan === 'string' ? response.subscriptionPlan : state.userSubscriptionPlan,
|
|
2392
|
+
userSubscriptionStatus: typeof response.subscriptionStatus === 'string' ? response.subscriptionStatus : state.userSubscriptionStatus,
|
|
2383
2393
|
userUsedTokens: typeof response.usedTokens === 'number' ? response.usedTokens : state.userUsedTokens
|
|
2384
2394
|
};
|
|
2385
2395
|
};
|
|
2386
2396
|
|
|
2387
2397
|
const isLoginResponse = value => {
|
|
2388
|
-
|
|
2389
|
-
return false;
|
|
2390
|
-
}
|
|
2391
|
-
return true;
|
|
2398
|
+
return !!value && typeof value === 'object';
|
|
2392
2399
|
};
|
|
2393
2400
|
|
|
2394
2401
|
const databaseName = 'auth-worker';
|
|
@@ -2445,6 +2452,15 @@ const setPersistentAuthValue = async (key, value) => {
|
|
|
2445
2452
|
await transactionToPromise(transaction);
|
|
2446
2453
|
};
|
|
2447
2454
|
|
|
2455
|
+
const persistLoginResult = async loginResult => {
|
|
2456
|
+
if (loginResult.userState !== 'loggedIn') {
|
|
2457
|
+
return loginResult;
|
|
2458
|
+
}
|
|
2459
|
+
await setPersistentAuthValue('accessToken', loginResult.authAccessToken ?? '');
|
|
2460
|
+
await setPersistentAuthValue('refreshToken', loginResult.authRefreshToken ?? '');
|
|
2461
|
+
return loginResult;
|
|
2462
|
+
};
|
|
2463
|
+
|
|
2448
2464
|
const getBackendOidcTokenUrl = backendUrl => {
|
|
2449
2465
|
return getBackendAuthUrl(backendUrl, '/oidc/token');
|
|
2450
2466
|
};
|
|
@@ -2500,14 +2516,6 @@ const waitForElectronBackendLogin = async (backendUrl, uid, redirectUri, codeVer
|
|
|
2500
2516
|
return getLoggedOutBackendAuthState('Timed out waiting for backend login.');
|
|
2501
2517
|
};
|
|
2502
2518
|
|
|
2503
|
-
const persistLoginResult = async loginResult => {
|
|
2504
|
-
if (loginResult.userState !== 'loggedIn') {
|
|
2505
|
-
return loginResult;
|
|
2506
|
-
}
|
|
2507
|
-
await setPersistentAuthValue('accessToken', loginResult.authAccessToken ?? '');
|
|
2508
|
-
await setPersistentAuthValue('refreshToken', loginResult.authRefreshToken ?? '');
|
|
2509
|
-
return loginResult;
|
|
2510
|
-
};
|
|
2511
2519
|
const handleClickLogin = async options => {
|
|
2512
2520
|
const {
|
|
2513
2521
|
authUseRedirect,
|