Can my code determine the creation date of the current user account?

If an administrator created a user account in the Firebase console after this user is logged in, is it possible to get the user creation date?

PS Big vote for Firebase to add more administrative controls programmatically, for user management.

+6
source share
4 answers

Currently AFAIK, getting the creation date is only possible with the Admin Node.js SDK:

admin.auth().getUser(uid) .then(function(userRecord) { console.log("Creation time:", userRecord.metadata.creationTime); }); 

Documentation: https://firebase.google.com/docs/reference/admin/node/admin.auth.UserMetadata#creationTime

+8
source

SDK administrative backend

Now this is possible using the following if you are trying to get information about the application on the server side.

 admin.auth().getUser(uid).then(user => { console.log(user.metadata.creationTime); }); 

Client side applications

Despite the fact that you can see this information on the Firebase Auth console, you cannot get this data on the application side, as you can see in the documentation .

If you want to use this data in your application, you will need to save it under your database, for example, databaseRoot/user/userUid/createdAt . Therefore, make sure that you create this node when creating a new user, for example, in this question .

+3
source

This function will iterate over all your users and write createDate to users/$uid/company location there.

 const iterateAllUsers = function () { const prom = db.ref('/users').once('value').then( (snap) => { const promArray = []; const users = snap.val(); Object.keys(users).forEach((user) => { promArray.push(getUIDCreationDate(user)); }); return Promise.all(promArray); }); return prom; } const getUIDCreationDate = function (uid) { const prom = fb.getUser(uid) .then(function (userRecord) { const prom2 = db.ref(`/users/${uid}/company`).update({ creationDate: userRecord.metadata.createdAt }).then((success) => console.log(success)).catch((error) => console.log(error)); return prom2; }).catch( error => { console.log(JSON.stringify(error)) }); return prom; } 
+1
source

There is a way to get this ... When you get firebase.User - usually from some code, such as:

 this.afAuth.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider()).then( (userCredential) => {//do something with user - notice that this is a user credential. }); 

anyway, there is a user inside this userCredential, and if you do

 let myObj = JSON.parse(JSON.stringify(userCredential.user); 

you will see that you can access the createAt field

 myObj.createdAt // somenumber = time in miliseconds since 1970 

So as to get access to it

 let myDate: Date = new Date(); myDate.setTime(+myObj.createdAt); //the + is important, to make sure it converts to number console.log("CREATED AT = " + myDate.toString() ); 

VOILA!

0
source

All Articles