zudoku 0.3.1-dev.16 → 0.3.1-dev.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zudoku",
3
- "version": "0.3.1-dev.16",
3
+ "version": "0.3.1-dev.17",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -116,7 +116,7 @@
116
116
  "openapi-types": "12.1.3",
117
117
  "picocolors": "^1.0.1",
118
118
  "postcss": "8.4.39",
119
- "posthog-node": "^4.0.1",
119
+ "posthog-node": "^4.1.1",
120
120
  "prism-react-renderer": "2.3.1",
121
121
  "prismjs": "1.29.0",
122
122
  "react": "18.3.1",
@@ -18,7 +18,7 @@ class Auth0AuthenticationProvider extends OpenIDAuthenticationProvider {
18
18
  isPending: false,
19
19
  profile: undefined,
20
20
  });
21
- localStorage.removeItem("auto-login");
21
+ sessionStorage.clear();
22
22
  const as = await this.getAuthServer();
23
23
 
24
24
  const redirectUrl = new URL(
@@ -44,7 +44,6 @@ export class OpenIDAuthenticationProvider implements AuthenticationProvider {
44
44
  protected tokenEndpoint: string | undefined;
45
45
 
46
46
  protected authorizationServer: oauth.AuthorizationServer | undefined;
47
- protected tokens: TokenState | undefined;
48
47
 
49
48
  protected callbackUrlPath = "/oauth/callback";
50
49
  protected logoutRedirectUrlPath = "/";
@@ -117,13 +116,13 @@ export class OpenIDAuthenticationProvider implements AuthenticationProvider {
117
116
  throw new AuthorizationError("No expires_in in response");
118
117
  }
119
118
 
120
- this.tokens = {
119
+ const tokens: TokenState = {
121
120
  accessToken: response.access_token,
122
121
  refreshToken: response.refresh_token,
123
122
  expiresOn: new Date(Date.now() + response.expires_in * 1000),
124
123
  tokenType: response.token_type,
125
124
  };
126
- sessionStorage.setItem("openid-token", JSON.stringify(this.tokens));
125
+ sessionStorage.setItem("token-state", JSON.stringify(tokens));
127
126
  }
128
127
 
129
128
  async signUp({ redirectTo }: { redirectTo?: string } = {}) {
@@ -210,11 +209,14 @@ export class OpenIDAuthenticationProvider implements AuthenticationProvider {
210
209
 
211
210
  async getAccessToken(): Promise<string> {
212
211
  const as = await this.getAuthServer();
213
- if (!this.tokens) {
212
+ const tokenState = sessionStorage.getItem("token-state");
213
+ if (!tokenState) {
214
214
  throw new AuthorizationError("User is not authenticated");
215
215
  }
216
- if (this.tokens.expiresOn < new Date()) {
217
- if (!this.tokens.refreshToken) {
216
+
217
+ const state = JSON.parse(tokenState) as TokenState;
218
+ if (state.expiresOn < new Date()) {
219
+ if (!state.refreshToken) {
218
220
  await this.signIn();
219
221
  return "";
220
222
  }
@@ -222,7 +224,7 @@ export class OpenIDAuthenticationProvider implements AuthenticationProvider {
222
224
  const request = await oauth.refreshTokenGrantRequest(
223
225
  as,
224
226
  this.client,
225
- this.tokens.refreshToken,
227
+ state.refreshToken,
226
228
  );
227
229
  const response = await oauth.processRefreshTokenResponse(
228
230
  as,
@@ -230,10 +232,16 @@ export class OpenIDAuthenticationProvider implements AuthenticationProvider {
230
232
  request,
231
233
  );
232
234
 
235
+ if (!response.access_token) {
236
+ throw new AuthorizationError("No access token in response");
237
+ }
238
+
233
239
  this.setTokensFromResponse(response);
234
- }
235
240
 
236
- return this.tokens.accessToken;
241
+ return response.access_token.toString();
242
+ } else {
243
+ return state.accessToken;
244
+ }
237
245
  }
238
246
 
239
247
  signOut = async () => {
@@ -242,7 +250,7 @@ export class OpenIDAuthenticationProvider implements AuthenticationProvider {
242
250
  isPending: false,
243
251
  profile: undefined,
244
252
  });
245
- localStorage.removeItem("auto-login");
253
+ sessionStorage.clear();
246
254
 
247
255
  const as = await this.getAuthServer();
248
256
 
@@ -349,20 +357,29 @@ export class OpenIDAuthenticationProvider implements AuthenticationProvider {
349
357
  profile,
350
358
  });
351
359
 
352
- localStorage.setItem("auto-login", "1");
360
+ sessionStorage.setItem(
361
+ "profile-state",
362
+ JSON.stringify(useAuthState.getState().profile),
363
+ );
353
364
 
354
- return sessionStorage.getItem("redirect-to") ?? "/";
365
+ const redirectTo = sessionStorage.getItem("redirect-to") ?? "/";
366
+ sessionStorage.removeItem("redirect-to");
367
+ return redirectTo;
355
368
  };
356
369
 
357
370
  pageLoad(): void {
358
- if (localStorage.getItem("auto-login")) {
359
- localStorage.removeItem("auto-login");
360
-
361
- // TODO: This needs to be cleaned up. We need to be able to return an
362
- // error to the user if the authentication fails.
363
- this.authorize({ redirectTo: window.location.pathname }).catch((err) => {
364
- logger.error(err);
365
- });
371
+ const profileState = sessionStorage.getItem("profile-state");
372
+ if (profileState) {
373
+ try {
374
+ const profile = JSON.parse(profileState);
375
+ useAuthState.setState({
376
+ isAuthenticated: true,
377
+ isPending: false,
378
+ profile,
379
+ });
380
+ } catch (err) {
381
+ logger.error("Error parsing auth state", err);
382
+ }
366
383
  }
367
384
  }
368
385