Skip to content

Commit

Permalink
fix: fixed beginResetPassword()
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed Jan 5, 2023
1 parent b6cfe21 commit fdce1b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
5 changes: 1 addition & 4 deletions packages/firebase-server/src/lib/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ export abstract class AbstractFirebaseServerAuthUserContext<S extends FirebaseSe
};

// set the claims
await this.updateClaims({
[FIREBASE_SERVER_AUTH_CLAIMS_RESET_PASSWORD_KEY]: null,
[FIREBASE_SERVER_AUTH_CLAIMS_RESET_LAST_COM_DATE_KEY]: null
});
await this.updateClaims(passwordClaimsData);

// update the user
await this.updateUser({ password });
Expand Down
51 changes: 51 additions & 0 deletions packages/firebase-server/src/lib/nest/auth/auth.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,57 @@ describe('firebase server nest auth', () => {
});
});

describe('loadDetails()', () => {
it('should load the details for the user.', async () => {
const details = await authUserContext.loadDetails();
expect(details).toBeDefined();
});
});

describe('beginResetPassword()', () => {
it('should add password reset claims to the user and change their password.', async () => {
let record = await authUserContext.loadRecord();
const passwordHash = record.passwordHash;
expect(passwordHash).not.toBeDefined(); // no password set in this test

let resetPasswordClaims = await authUserContext.loadResetPasswordClaims();
expect(resetPasswordClaims).toBeUndefined();

await authUserContext.beginResetPassword();

authUserContext = authService.userContext(u.uid);
record = await authUserContext.loadRecord();

expect(record.passwordHash).not.toBe(passwordHash);

resetPasswordClaims = await authUserContext.loadResetPasswordClaims();
expect(resetPasswordClaims).toBeDefined();
});
});

describe('setPassword()', () => {
it('should clear any reset password claims.', async () => {
await authUserContext.beginResetPassword();

authUserContext = authService.userContext(u.uid);
let record = await authUserContext.loadRecord();

expect(record.passwordHash).toBeDefined();

let resetPasswordClaims = await authUserContext.loadResetPasswordClaims();
expect(resetPasswordClaims).toBeDefined();

// set new password
await authUserContext.setPassword('newpassword');

authUserContext = authService.userContext(u.uid);
record = await authUserContext.loadRecord();

resetPasswordClaims = await authUserContext.loadResetPasswordClaims();
expect(resetPasswordClaims).not.toBeDefined();
});
});

describe('addRoles()', () => {
it('should update the claims to have the roles (as configured by the service).', async () => {
await authUserContext.addRoles(AUTH_ADMIN_ROLE);
Expand Down

0 comments on commit fdce1b8

Please sign in to comment.