Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can my code determine the creation date for the current user's account?

If an admin has created a user account in the Firebase Console, after this user has signed in, is it possible to retrieve the 'created' date of the user?

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

like image 290
Josh Kahane Avatar asked Jun 21 '16 19:06

Josh Kahane


4 Answers

Currently AFAIK, getting 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/firebase-admin.auth.usermetadata.md#usermetadatacreationtime

like image 69
Ryan Avatar answered Nov 09 '22 14:11

Ryan


On the Firebase client web API you can get the user's account creation date with:

var user = firebase.auth().currentUser;
var signupDate = new Date(user.metadata.creationTime);

Link to unhelpful documentation.

like image 31
Robin Stewart Avatar answered Nov 09 '22 13:11

Robin Stewart


Admin backend SDK

This is now achievable with the following in case you are trying to get the info on a server side application.

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

Client side Applications

Despite you are able to see this information on firebase Auth console you wont be able to retrieve this data on the application side as you can see in the documentation.

If you want to use this data on your application you'll need to store it under your database on somethink like databaseRoot/user/userUid/createdAt. So make sure you are creating this node whenever creating a new user such as in this question.

like image 6
adolfosrs Avatar answered Nov 09 '22 14:11

adolfosrs


As of this writing, Angularfire2 is at release candidate version 5. The Javascript API makes it possible to retrieve both the creation date and the last login date of the currently authenticated user.

Example:

this.afAuth.auth.onAuthStateChanged(user => {
  const createdAt = user.metadata.creationTime
  const lastLogin = user.metadata.lastSignInTime
  const a = user.metadata['a'] // <- Typescript does not allow '.' operator
  const b = user.metadata['b'] // <- Typescript does not allow '.' operator

  console.log(`${ user.email } was created ${ createdAt } (${ a }).`)
  console.log(`${ user.email } last logged in ${ lastLogin } (${ b }).`)
})

Though not listed as formal properties, a and b yield the Date objects for the creation and last login dates, respectively, while creationTime and lastSignInTime are the same as GMT string values.

like image 3
eppineda Avatar answered Nov 09 '22 12:11

eppineda