1- // The MIT License (MIT)
2- //
3- // Copyright (c) 2017 Firebase
4- //
5- // Permission is hereby granted, free of charge, to any person obtaining a copy
6- // of this software and associated documentation files (the "Software"), to deal
7- // in the Software without restriction, including without limitation the rights
8- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9- // copies of the Software, and to permit persons to whom the Software is
10- // furnished to do so, subject to the following conditions:
11- //
12- // The above copyright notice and this permission notice shall be included in all
13- // copies or substantial portions of the Software.
14- //
15- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21- // SOFTWARE.
1+ // The MIT License (MIT)
2+ //
3+ // Copyright (c) 2017 Firebase
4+ //
5+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6+ // of this software and associated documentation files (the "Software"), to deal
7+ // in the Software without restriction, including without limitation the rights
8+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ // copies of the Software, and to permit persons to whom the Software is
10+ // furnished to do so, subject to the following conditions:
11+ //
12+ // The above copyright notice and this permission notice shall be included in all
13+ // copies or substantial portions of the Software.
14+ //
15+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+ // SOFTWARE.
2222
2323import { makeCloudFunction , CloudFunction , Event } from '../cloud-functions' ;
2424import * as firebase from 'firebase-admin' ;
@@ -33,6 +33,26 @@ export function user() {
3333
3434/** Builder used to create Cloud Functions for Firebase Auth user lifecycle events. */
3535export class UserBuilder {
36+ private static dataConstructor ( raw : any ) : firebase . auth . UserRecord {
37+ // The UserRecord returned here is an interface. The firebase-admin/auth/user-record module
38+ // also has a class of the same name, which is one implementation of the interface. Here,
39+ // because our wire format already almost matches the UserRecord interface, we only use the
40+ // interface, no need to use the class.
41+ //
42+ // The one change we need to make to match the interface is to our incoming timestamps. The
43+ // interface requires them to be Date objects, while they raw payload has them as strings.
44+ if ( raw . data . metadata ) {
45+ let metadata = raw . data . metadata ;
46+ if ( metadata . lastSignedInAt && typeof metadata . lastSignedInAt === 'string' ) {
47+ metadata . lastSignedInAt = new Date ( metadata . lastSignedInAt ) ;
48+ }
49+ if ( metadata . createdAt && typeof metadata . createdAt === 'string' ) {
50+ metadata . createdAt = new Date ( metadata . createdAt ) ;
51+ }
52+ }
53+ return raw . data ;
54+ }
55+
3656 /** @internal */
3757 constructor ( private resource : string ) { }
3858
@@ -44,6 +64,7 @@ export class UserBuilder {
4464 provider, handler,
4565 resource : this . resource ,
4666 eventType : 'user.create' ,
67+ dataConstructor : UserBuilder . dataConstructor ,
4768 } ) ;
4869 }
4970
@@ -55,12 +76,13 @@ export class UserBuilder {
5576 provider, handler,
5677 resource : this . resource ,
5778 eventType : 'user.delete' ,
79+ dataConstructor : UserBuilder . dataConstructor ,
5880 } ) ;
5981 }
6082}
6183
62- /**
63- * The UserRecord passed to Cloud Functions is the same UserRecord that is returned by the Firebase Admin
64- * SDK.
65- */
84+ /**
85+ * The UserRecord passed to Cloud Functions is the same UserRecord that is returned by the Firebase Admin
86+ * SDK.
87+ */
6688export type UserRecord = firebase . auth . UserRecord ;
0 commit comments