Skip to content

Commit

Permalink
fix(auth): Properly parse the lastRefreshTime. (#888)
Browse files Browse the repository at this point in the history
It's returned from the server in a different format than the other timestamps.

Fixes #887
  • Loading branch information
rsgowman committed May 12, 2020
1 parent 090adb9 commit d985602
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/auth/user-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ export class UserMetadata {
// These bugs have already been addressed since then.
utils.addReadonlyGetter(this, 'creationTime', parseDate(response.createdAt));
utils.addReadonlyGetter(this, 'lastSignInTime', parseDate(response.lastLoginAt));
utils.addReadonlyGetter(this, 'lastRefreshTime', parseDate(response.lastRefreshAt));
const lastRefreshAt = response.lastRefreshAt ? new Date(response.lastRefreshAt).toUTCString() : null;
utils.addReadonlyGetter(this, 'lastRefreshTime', lastRefreshAt);
}

/** @return The plain object representation of the user's metadata. */
Expand Down
4 changes: 4 additions & 0 deletions test/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ describe('admin.auth', () => {
.then((userRecord) => {
expect(userRecord.metadata.lastRefreshTime).to.exist;
expect(isUTCString(userRecord.metadata.lastRefreshTime!));
const creationTime = new Date(userRecord.metadata.creationTime).getTime();
const lastRefreshTime = new Date(userRecord.metadata.lastRefreshTime!).getTime();
expect(creationTime).lte(lastRefreshTime);
expect(lastRefreshTime).lte(creationTime + 3600 * 1000);
});
} finally {
admin.auth().deleteUser('lastRefreshTimeUser');
Expand Down
6 changes: 6 additions & 0 deletions test/unit/auth/user-record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,12 @@ describe('UserInfo', () => {
describe('UserMetadata', () => {
const expectedLastLoginAt = 1476235905000;
const expectedCreatedAt = 1476136676000;
const expectedLastRefreshAt = '2016-10-12T01:31:45.000Z';
const actualMetadata: UserMetadata = new UserMetadata({
localId: 'uid123',
lastLoginAt: expectedLastLoginAt.toString(),
createdAt: expectedCreatedAt.toString(),
lastRefreshAt: expectedLastRefreshAt,
});
const expectedMetadataJSON = {
lastSignInTime: new Date(expectedLastLoginAt).toUTCString(),
Expand Down Expand Up @@ -676,6 +678,10 @@ describe('UserMetadata', () => {
(actualMetadata as any).creationTime = new Date();
}).to.throw(Error);
});

it('should return expected lastRefreshTime', () => {
expect(actualMetadata.lastRefreshTime).to.equal(new Date(expectedLastRefreshAt).toUTCString())
});
});

describe('toJSON', () => {
Expand Down

0 comments on commit d985602

Please sign in to comment.