Skip to content

Commit 3a7c4a3

Browse files
authored
Define validatePassword endpoint for public API (#7409)
* Add validatePassword method with implementation TODO
1 parent 8be31e5 commit 3a7c4a3

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

common/api-review/auth.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,9 @@ export interface UserMetadata {
893893
// @public
894894
export type UserProfile = Record<string, unknown>;
895895

896+
// @public
897+
export function validatePassword(auth: Auth, password: string): Promise<PasswordValidationStatus>;
898+
896899
// @public
897900
export function verifyBeforeUpdateEmail(user: User, newEmail: string, actionCodeSettings?: ActionCodeSettings | null): Promise<void>;
898901

docs-devsite/auth.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Firebase Authentication
4949
| [signOut(auth)](./auth.md#signout) | Signs out the current user. |
5050
| [updateCurrentUser(auth, user)](./auth.md#updatecurrentuser) | Asynchronously sets the provided user as [Auth.currentUser](./auth.auth.md#authcurrentuser) on the [Auth](./auth.auth.md#auth_interface) instance. |
5151
| [useDeviceLanguage(auth)](./auth.md#usedevicelanguage) | Sets the current language to the default device/browser preference. |
52+
| [validatePassword(auth, password)](./auth.md#validatepassword) | Validates the password against the password policy configured for the project or tenant. |
5253
| [verifyPasswordResetCode(auth, code)](./auth.md#verifypasswordresetcode) | Checks a password reset code sent to the user by email or other out-of-band mechanism. |
5354
| <b>function(link...)</b> |
5455
| [parseActionCodeURL(link)](./auth.md#parseactioncodeurl) | Parses the email action link string and returns an [ActionCodeURL](./auth.actioncodeurl.md#actioncodeurl_class) if the link is valid, otherwise returns null. |
@@ -1080,6 +1081,39 @@ export declare function useDeviceLanguage(auth: Auth): void;
10801081

10811082
void
10821083

1084+
## validatePassword()
1085+
1086+
Validates the password against the password policy configured for the project or tenant.
1087+
1088+
If no tenant ID is set on the `Auth` instance, then this method will use the password policy configured for the project. Otherwise, this method will use the policy configured for the tenant. If a password policy has not been configured, then the default policy configured for all projects will be used.
1089+
1090+
If an auth flow fails because a submitted password does not meet the password policy requirements and this method has previously been called, then this method will use the most recent policy available when called again.
1091+
1092+
<b>Signature:</b>
1093+
1094+
```typescript
1095+
export declare function validatePassword(auth: Auth, password: string): Promise<PasswordValidationStatus>;
1096+
```
1097+
1098+
### Parameters
1099+
1100+
| Parameter | Type | Description |
1101+
| --- | --- | --- |
1102+
| auth | [Auth](./auth.auth.md#auth_interface) | The [Auth](./auth.auth.md#auth_interface) instance. |
1103+
| password | string | The password to validate. |
1104+
1105+
<b>Returns:</b>
1106+
1107+
Promise&lt;[PasswordValidationStatus](./auth.passwordvalidationstatus.md#passwordvalidationstatus_interface)<!-- -->&gt;
1108+
1109+
### Example
1110+
1111+
1112+
```javascript
1113+
validatePassword(auth, 'some-password');
1114+
1115+
```
1116+
10831117
## verifyPasswordResetCode()
10841118

10851119
Checks a password reset code sent to the user by email or other out-of-band mechanism.

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
ErrorFn,
3333
NextFn,
3434
Unsubscribe,
35-
PasswordPolicy
35+
PasswordPolicy,
36+
PasswordValidationStatus
3637
} from '../../model/public_types';
3738
import {
3839
createSubscribe,
@@ -427,6 +428,11 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
427428
}
428429
}
429430

431+
async validatePassword(password: string): Promise<PasswordValidationStatus> {
432+
// TODO(chazzy): Implement.
433+
return Promise.reject(password);
434+
}
435+
430436
_getPasswordPolicy(): PasswordPolicy | null {
431437
if (this.tenantId === null) {
432438
return this._projectPasswordPolicy;

packages/auth/src/core/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
User,
2424
CompleteFn,
2525
ErrorFn,
26-
Unsubscribe
26+
Unsubscribe,
27+
PasswordValidationStatus
2728
} from '../model/public_types';
2829
import { _castAuth } from '../core/auth/auth_impl';
2930

@@ -96,6 +97,37 @@ export function initializeRecaptchaConfig(auth: Auth): Promise<void> {
9697
return authInternal.initializeRecaptchaConfig();
9798
}
9899

100+
/**
101+
* Validates the password against the password policy configured for the project or tenant.
102+
*
103+
* @remarks
104+
* If no tenant ID is set on the `Auth` instance, then this method will use the password
105+
* policy configured for the project. Otherwise, this method will use the policy configured
106+
* for the tenant. If a password policy has not been configured, then the default policy
107+
* configured for all projects will be used.
108+
*
109+
* If an auth flow fails because a submitted password does not meet the password policy
110+
* requirements and this method has previously been called, then this method will use the
111+
* most recent policy available when called again.
112+
*
113+
* @example
114+
* ```javascript
115+
* validatePassword(auth, 'some-password');
116+
* ```
117+
*
118+
* @param auth The {@link Auth} instance.
119+
* @param password The password to validate.
120+
*
121+
* @public
122+
*/
123+
export async function validatePassword(
124+
auth: Auth,
125+
password: string
126+
): Promise<PasswordValidationStatus> {
127+
const authInternal = _castAuth(auth);
128+
return authInternal.validatePassword(password);
129+
}
130+
99131
/**
100132
* Adds an observer for changes to the signed-in user's ID token.
101133
*

packages/auth/src/model/auth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Config,
2222
EmulatorConfig,
2323
PasswordPolicy,
24+
PasswordValidationStatus,
2425
PopupRedirectResolver,
2526
User
2627
} from './public_types';
@@ -103,4 +104,5 @@ export interface AuthInternal extends Auth {
103104
useDeviceLanguage(): void;
104105
signOut(): Promise<void>;
105106
initializeRecaptchaConfig(): Promise<void>;
107+
validatePassword(password: string): Promise<PasswordValidationStatus>;
106108
}

0 commit comments

Comments
 (0)