auth-vir 2.0.3 → 2.0.4

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.
@@ -31,8 +31,12 @@ export declare class FrontendAuthClient<AssumedUserParams extends JsonCompatible
31
31
  constructor(config?: FrontendAuthClientConfig);
32
32
  /** Wraps {@link getCurrentCsrfToken} to automatically handle wiping an invalid CSRF token. */
33
33
  getCurrentCsrfToken(): Promise<string | undefined>;
34
- /** @returns Whether the user assuming succeeded or not. */
35
- assumeUser(assumedUserParams: Readonly<AssumedUserParams>): Promise<boolean>;
34
+ /**
35
+ * Assume the given user. Pass `undefined` to wipe the currently assumed user.
36
+ *
37
+ * @returns Whether the assumed user setting or clearing succeeded or not.
38
+ */
39
+ assumeUser(assumedUserParams: Readonly<AssumedUserParams> | undefined): Promise<boolean>;
36
40
  /** Gets the assumed user params stored in local storage, if any. */
37
41
  getAssumedUser(): AssumedUserParams | undefined;
38
42
  /**
@@ -25,12 +25,22 @@ export class FrontendAuthClient {
25
25
  return csrfTokenResult.csrfToken?.token;
26
26
  }
27
27
  }
28
- /** @returns Whether the user assuming succeeded or not. */
28
+ /**
29
+ * Assume the given user. Pass `undefined` to wipe the currently assumed user.
30
+ *
31
+ * @returns Whether the assumed user setting or clearing succeeded or not.
32
+ */
29
33
  async assumeUser(assumedUserParams) {
34
+ const localStorage = this.config.overrides?.localStorage || globalThis.localStorage;
35
+ const storageKey = this.config.overrides?.assumedUserHeaderName || AuthHeaderName.AssumedUser;
36
+ if (!assumedUserParams) {
37
+ localStorage.removeItem(storageKey);
38
+ return true;
39
+ }
30
40
  if (!(await this.config.canAssumeUser?.())) {
31
41
  return false;
32
42
  }
33
- (this.config.overrides?.localStorage || globalThis.localStorage).setItem(this.config.overrides?.assumedUserHeaderName || AuthHeaderName.AssumedUser, JSON.stringify(assumedUserParams));
43
+ localStorage.setItem(storageKey, JSON.stringify(assumedUserParams));
34
44
  return true;
35
45
  }
36
46
  /** Gets the assumed user params stored in local storage, if any. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auth-vir",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Auth made easy and secure via JWT cookies, CSRF tokens, and password hashing helpers.",
5
5
  "keywords": [
6
6
  "auth",
@@ -60,16 +60,28 @@ export class FrontendAuthClient<AssumedUserParams extends JsonCompatibleObject =
60
60
  }
61
61
  }
62
62
 
63
- /** @returns Whether the user assuming succeeded or not. */
64
- public async assumeUser(assumedUserParams: Readonly<AssumedUserParams>): Promise<boolean> {
63
+ /**
64
+ * Assume the given user. Pass `undefined` to wipe the currently assumed user.
65
+ *
66
+ * @returns Whether the assumed user setting or clearing succeeded or not.
67
+ */
68
+ public async assumeUser(
69
+ assumedUserParams: Readonly<AssumedUserParams> | undefined,
70
+ ): Promise<boolean> {
71
+ const localStorage = this.config.overrides?.localStorage || globalThis.localStorage;
72
+ const storageKey =
73
+ this.config.overrides?.assumedUserHeaderName || AuthHeaderName.AssumedUser;
74
+
75
+ if (!assumedUserParams) {
76
+ localStorage.removeItem(storageKey);
77
+ return true;
78
+ }
79
+
65
80
  if (!(await this.config.canAssumeUser?.())) {
66
81
  return false;
67
82
  }
68
83
 
69
- (this.config.overrides?.localStorage || globalThis.localStorage).setItem(
70
- this.config.overrides?.assumedUserHeaderName || AuthHeaderName.AssumedUser,
71
- JSON.stringify(assumedUserParams),
72
- );
84
+ localStorage.setItem(storageKey, JSON.stringify(assumedUserParams));
73
85
 
74
86
  return true;
75
87
  }