mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00:00
feat(set-initial-password): [Auth/PM-18784] SetInitialPasswordComponent Handle TDE Offboarding (#14861)
This PR makes it so that `SetInitialPasswordComponent` handles the TDE offboarding flow where an org user now needs to set an initial master password. Feature flag: `PM16117_SetInitialPasswordRefactor`
This commit is contained in:
45
libs/common/src/auth/utils/assert-non-nullish.util.ts
Normal file
45
libs/common/src/auth/utils/assert-non-nullish.util.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Asserts that a value is non-nullish (not `null` or `undefined`); throws if value is nullish.
|
||||
*
|
||||
* @param val the value to check
|
||||
* @param name the name of the value to include in the error message
|
||||
* @param ctx context to optionally append to the error message
|
||||
* @throws if the value is null or undefined
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```
|
||||
* // `newPasswordHint` can have an empty string as a valid value, so we check non-nullish
|
||||
* this.assertNonNullish(
|
||||
* passwordInputResult.newPasswordHint,
|
||||
* "newPasswordHint",
|
||||
* "Could not set initial password."
|
||||
* );
|
||||
* // Output error message: "newPasswordHint is null or undefined. Could not set initial password."
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* If you use this method repeatedly to check several values, it may help to assign any
|
||||
* additional context (`ctx`) to a variable and pass it in to each call. This prevents the
|
||||
* call from reformatting vertically via prettier in your text editor, taking up multiple lines.
|
||||
*
|
||||
* For example:
|
||||
* ```
|
||||
* const ctx = "Could not set initial password.";
|
||||
*
|
||||
* this.assertNonNullish(valueOne, "valueOne", ctx);
|
||||
* this.assertNonNullish(valueTwo, "valueTwo", ctx);
|
||||
* this.assertNonNullish(valueThree, "valueThree", ctx);
|
||||
* ```
|
||||
*/
|
||||
export function assertNonNullish<T>(
|
||||
val: T,
|
||||
name: string,
|
||||
ctx?: string,
|
||||
): asserts val is NonNullable<T> {
|
||||
if (val == null) {
|
||||
// If context is provided, append it to the error message with a space before it.
|
||||
throw new Error(`${name} is null or undefined.${ctx ? ` ${ctx}` : ""}`);
|
||||
}
|
||||
}
|
||||
46
libs/common/src/auth/utils/assert-truthy.util.ts
Normal file
46
libs/common/src/auth/utils/assert-truthy.util.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Asserts that a value is truthy; throws if value is falsy.
|
||||
*
|
||||
* @param val the value to check
|
||||
* @param name the name of the value to include in the error message
|
||||
* @param ctx context to optionally append to the error message
|
||||
* @throws if the value is falsy (`false`, `""`, `0`, `null`, `undefined`, `void`, or `NaN`)
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```
|
||||
* this.assertTruthy(
|
||||
* this.organizationId,
|
||||
* "organizationId",
|
||||
* "Could not set initial password."
|
||||
* );
|
||||
* // Output error message: "organizationId is falsy. Could not set initial password."
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* If you use this method repeatedly to check several values, it may help to assign any
|
||||
* additional context (`ctx`) to a variable and pass it in to each call. This prevents the
|
||||
* call from reformatting vertically via prettier in your text editor, taking up multiple lines.
|
||||
*
|
||||
* For example:
|
||||
* ```
|
||||
* const ctx = "Could not set initial password.";
|
||||
*
|
||||
* this.assertTruthy(valueOne, "valueOne", ctx);
|
||||
* this.assertTruthy(valueTwo, "valueTwo", ctx);
|
||||
* this.assertTruthy(valueThree, "valueThree", ctx);
|
||||
*/
|
||||
export function assertTruthy<T>(
|
||||
val: T,
|
||||
name: string,
|
||||
ctx?: string,
|
||||
): asserts val is Exclude<T, false | "" | 0 | null | undefined | void | 0n> {
|
||||
// Because `NaN` is a value (not a type) of type 'number', that means we cannot add
|
||||
// it to the list of falsy values in the type assertion. Instead, we check for it
|
||||
// separately at runtime.
|
||||
if (!val || (typeof val === "number" && Number.isNaN(val))) {
|
||||
// If context is provided, append it to the error message with a space before it.
|
||||
throw new Error(`${name} is falsy.${ctx ? ` ${ctx}` : ""}`);
|
||||
}
|
||||
}
|
||||
2
libs/common/src/auth/utils/index.ts
Normal file
2
libs/common/src/auth/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { assertTruthy } from "./assert-truthy.util";
|
||||
export { assertNonNullish } from "./assert-non-nullish.util";
|
||||
Reference in New Issue
Block a user