Reputation: 17169
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.
Upvotes: 26
Views: 17534
Reputation: 606
I used this function for my Xamarin Android project to get the date and time which worked for me:
public DateTime GetCurrentUserCreatedDate()
{
var metadata = Firebase.Auth.FirebaseAuth.Instance.CurrentUser.Metadata;
var creationTimestamp = metadata?.CreationTimestamp;
var creationDate = DateTimeOffset.FromUnixTimeSeconds(creationTimestamp.Value / 1000).DateTime;
return creationDate;
}
Upvotes: 0
Reputation: 3903
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.
Upvotes: 23
Reputation: 1382
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
Upvotes: 12
Reputation: 653
As of Aug 2021, console.dir(user) gives:
{
"uid": "2Sf.......PH2",
"anonymous": true,
"isAnonymous": true,
"providers": [],
"photoURL": null,
"email": null,
"emailVerified": false,
"displayName": null,
"phoneNumber": null,
"refreshToken": "ACz.......7tgTs",
"metadata": {
"creationTimestamp": "2020-07-08T18:32:09.701Z",
"lastSignInTimestamp": "2020-07-08T18:32:09.701Z"
}
}
So, user.metadata.creationTimestamp
will give the creation date.
Upvotes: 1
Reputation: 687
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.
Upvotes: 3
Reputation: 213
There is a way of getting it... When you get a 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.
});
anyways, inside this userCredential there is the user and if you do
let myObj = JSON.parse(JSON.stringify(userCredential.user);
you are going to see that you can access the createdAt field
myObj.createdAt // somenumber = time in miliseconds since 1970
So to access 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!
Upvotes: 1
Reputation: 9389
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.
Upvotes: 8
Reputation: 41
This function will iterate through all of your users and record there creationDate under the users/$uid/company
location
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;
}
Upvotes: 2