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.
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
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.
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);
});
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With