@lvce-editor/auth-worker 1.1.0 → 1.3.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.
@@ -997,10 +997,7 @@ const getLoggedOutBackendAuthState = (authErrorMessage = '') => {
997
997
  return {
998
998
  authAccessToken: '',
999
999
  authErrorMessage,
1000
- userName: '',
1001
- userState: 'loggedOut',
1002
- userSubscriptionPlan: '',
1003
- userUsedTokens: 0
1000
+ userState: 'loggedOut'
1004
1001
  };
1005
1002
  };
1006
1003
 
@@ -1029,6 +1026,57 @@ const getBackendRefreshUrl = backendUrl => {
1029
1026
  return getBackendAuthUrl(backendUrl, '/auth/refresh');
1030
1027
  };
1031
1028
 
1029
+ const delay = async ms => {
1030
+ await new Promise(resolve => setTimeout(resolve, ms));
1031
+ };
1032
+
1033
+ let nextLoginResponse;
1034
+ let nextRefreshResponse;
1035
+ const setNextLoginResponse = response => {
1036
+ nextLoginResponse = response;
1037
+ };
1038
+ const setNextRefreshResponse = response => {
1039
+ nextRefreshResponse = response;
1040
+ };
1041
+ const clear = () => {
1042
+ nextLoginResponse = undefined;
1043
+ nextRefreshResponse = undefined;
1044
+ };
1045
+ const hasPendingMockLoginResponse = () => {
1046
+ return !!nextLoginResponse;
1047
+ };
1048
+ const hasPendingMockRefreshResponse = () => {
1049
+ return !!nextRefreshResponse;
1050
+ };
1051
+ const consumeNextLoginResponse = async () => {
1052
+ if (!nextLoginResponse) {
1053
+ return undefined;
1054
+ }
1055
+ const response = nextLoginResponse;
1056
+ nextLoginResponse = undefined;
1057
+ if (response.delay > 0) {
1058
+ await delay(response.delay);
1059
+ }
1060
+ if (response.type === 'error') {
1061
+ throw new Error(response.message);
1062
+ }
1063
+ return response.response;
1064
+ };
1065
+ const consumeNextRefreshResponse = async () => {
1066
+ if (!nextRefreshResponse) {
1067
+ return undefined;
1068
+ }
1069
+ const response = nextRefreshResponse;
1070
+ nextRefreshResponse = undefined;
1071
+ if (response.delay > 0) {
1072
+ await new Promise(resolve => setTimeout(resolve, response.delay));
1073
+ }
1074
+ if (response.type === 'error') {
1075
+ throw new Error(response.message);
1076
+ }
1077
+ return response.response;
1078
+ };
1079
+
1032
1080
  const isObject = value => {
1033
1081
  return !!value && typeof value === 'object';
1034
1082
  };
@@ -1068,6 +1116,10 @@ const syncBackendAuth = async backendUrl => {
1068
1116
  return getLoggedOutBackendAuthState('Backend URL is missing.');
1069
1117
  }
1070
1118
  try {
1119
+ if (hasPendingMockRefreshResponse()) {
1120
+ const mockResponse = await consumeNextRefreshResponse();
1121
+ return parseBackendAuthResponse(mockResponse);
1122
+ }
1071
1123
  const response = await fetch(getBackendRefreshUrl(backendUrl), {
1072
1124
  credentials: 'include',
1073
1125
  headers: {
@@ -1102,10 +1154,6 @@ const syncBackendAuth = async backendUrl => {
1102
1154
  }
1103
1155
  };
1104
1156
 
1105
- const delay = async ms => {
1106
- await new Promise(resolve => setTimeout(resolve, ms));
1107
- };
1108
-
1109
1157
  const waitForBackendLogin = async (backendUrl, timeoutMs = 30_000, pollIntervalMs = 1000) => {
1110
1158
  const deadline = Date.now() + timeoutMs;
1111
1159
  let lastErrorMessage = '';
@@ -1142,38 +1190,18 @@ const isLoginResponse = value => {
1142
1190
  return true;
1143
1191
  };
1144
1192
 
1145
- let nextLoginResponse;
1146
- const clear = () => {
1147
- nextLoginResponse = undefined;
1148
- };
1149
- const hasPendingMockLoginResponse = () => {
1150
- return !!nextLoginResponse;
1151
- };
1152
- const consumeNextLoginResponse = async () => {
1153
- if (!nextLoginResponse) {
1154
- return undefined;
1155
- }
1156
- const response = nextLoginResponse;
1157
- nextLoginResponse = undefined;
1158
- if (response.delay > 0) {
1159
- await delay(response.delay);
1160
- }
1161
- if (response.type === 'error') {
1162
- throw new Error(response.message);
1163
- }
1164
- return response.response;
1165
- };
1166
-
1167
- const handleClickLogin = async state => {
1168
- if (!state.backendUrl) {
1193
+ const handleClickLogin = async options => {
1194
+ const {
1195
+ backendUrl,
1196
+ platform
1197
+ } = options;
1198
+ if (!backendUrl) {
1169
1199
  return {
1170
- ...state,
1171
1200
  authErrorMessage: 'Backend URL is missing.',
1172
1201
  userState: 'loggedOut'
1173
1202
  };
1174
1203
  }
1175
1204
  const signingInState = {
1176
- ...state,
1177
1205
  authErrorMessage: '',
1178
1206
  userState: 'loggingIn'
1179
1207
  };
@@ -1182,24 +1210,21 @@ const handleClickLogin = async state => {
1182
1210
  const response = await consumeNextLoginResponse();
1183
1211
  if (!isLoginResponse(response)) {
1184
1212
  return {
1185
- ...signingInState,
1186
1213
  authErrorMessage: 'Backend returned an invalid login response.',
1187
1214
  userState: 'loggedOut'
1188
1215
  };
1189
1216
  }
1190
1217
  if (typeof response.error === 'string' && response.error) {
1191
1218
  return {
1192
- ...signingInState,
1193
1219
  authErrorMessage: response.error,
1194
1220
  userState: 'loggedOut'
1195
1221
  };
1196
1222
  }
1197
1223
  return getLoggedInState(signingInState, response);
1198
1224
  }
1199
- await openUrl(getBackendLoginUrl(state.backendUrl), state.platform);
1200
- const authState = await waitForBackendLogin(state.backendUrl);
1225
+ await openUrl(getBackendLoginUrl(backendUrl), platform);
1226
+ const authState = await waitForBackendLogin(backendUrl);
1201
1227
  return {
1202
- ...signingInState,
1203
1228
  ...authState
1204
1229
  };
1205
1230
  } catch (error) {
@@ -1225,9 +1250,16 @@ const logout = async state => {
1225
1250
 
1226
1251
  const commandMap = {
1227
1252
  'Auth.clearMocks': clear,
1253
+ 'Auth.consumeNextLoginResponse': consumeNextLoginResponse,
1254
+ 'Auth.consumeNextRefreshResponse': consumeNextRefreshResponse,
1255
+ 'Auth.hasPendingMockLoginResponse': hasPendingMockLoginResponse,
1256
+ 'Auth.hasPendingMockRefreshResponse': hasPendingMockRefreshResponse,
1228
1257
  'Auth.initialize': initialize,
1229
1258
  'Auth.login': handleClickLogin,
1230
1259
  'Auth.logout': logout,
1260
+ 'Auth.setNextLoginResponse': setNextLoginResponse,
1261
+ 'Auth.setNextRefreshResponse': setNextRefreshResponse,
1262
+ 'Auth.syncBackendAuth': syncBackendAuth,
1231
1263
  'HandleMessagePort.handleMessagePort': handleMessagePort
1232
1264
  };
1233
1265
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/auth-worker",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Auth Worker",
5
5
  "repository": {
6
6
  "type": "git",