Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directly initialize properties in UserMetadata #803

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/auth/user-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,19 @@ export interface CreateRequest extends UpdateRequest {
* @constructor
*/
export class UserMetadata {
public readonly creationTime: string;
public readonly lastSignInTime: string;
public readonly creationTime: string | null;
public readonly lastSignInTime: string | null;

constructor(response: any) {
// Creation date should always be available but due to some backend bugs there
// were cases in the past where users did not have creation date properly set.
// This included legacy Firebase migrating project users and some anonymous users.
// These bugs have already been addressed since then.
utils.addReadonlyGetter(this, 'creationTime', parseDate(response.createdAt));
utils.addReadonlyGetter(this, 'lastSignInTime', parseDate(response.lastLoginAt));
this.creationTime = parseDate(response.createdAt);
utils.enforceReadonly(this, 'creationTime');

this.lastSignInTime = parseDate(response.lastLoginAt);
utils.enforceReadonly(this, 'lastSignInTime');
}

/** @return {object} The plain object representation of the user's metadata. */
Expand Down
5 changes: 2 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,12 @@ declare namespace admin.auth {
/**
* The date the user last signed in, formatted as a UTC string.
*/
lastSignInTime: string;
lastSignInTime: string | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Let's also update the doc comment to indicate when these values are null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Though note that there's not a great explanation as to why creationTime might be null. As an alternative, we could keep it as non-nullable and change the implementation to throw an internal error if this situation occurs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like the right approach to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. A number of unit tests needed to be changed as a result. I think the changes are correct, but definitely will want Bassam to weigh in on this too.


/**
* The date the user was created, formatted as a UTC string.
*
*/
creationTime: string;
creationTime: string | null;

/**
* @return A JSON-serializable representation of this object.
Expand Down
14 changes: 14 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ export function addReadonlyGetter(obj: object, prop: string, value: any): void {
});
}

/**
* Marks an existing property as readonly. Unlike typescript's "readonly"
* modifier, this will take effect at runtime too (generating a TypeError if
* violated), including when called from javascript.
*/
export function enforceReadonly(obj: object, prop: string): void {
Object.defineProperty(obj, prop, {
// Make this property read-only.
writable: false,
// Include this property during enumeration of obj's properties.
enumerable: true,
});
}

/**
* Returns the Google Cloud project ID associated with a Firebase app, if it's explicitly
* specified in either the Firebase app options, credentials or the local environment.
Expand Down