Get Created date and last login date from FIRUser with Firebase 3

13,209

Solution 1

You can, starting with version 4.4.0, get the last sign in time and creation time in iOS from the currentUser: https://firebase.google.com/support/release-notes/ios#4.4.0

Auth.auth().currentUser.metadata.lastSignInDate Auth.auth().currentUser.metadata.creationDate

Solution 2

In FIRUserMetadata your can find both properties:

creationDate

/** @property creationDate
    @brief Stores the creation date for the corresponding Firebase user.
 */
@property (copy, nonatomic, readonly, nullable) NSDate *creationDate;

to access this property use

let creationDate = Auth.auth().currentUser?.metadata.creationDate

lastSingInDate

/** @property lastSignInDate
    @brief Stores the last sign in date for the corresponding Firebase user.
 */
@property (copy, nonatomic, readonly, nullable) NSDate *lastSignInDate;

to access this property use

let lastSignInDate = Auth.auth().currentUser?.metadata.lastSignInDate

Solution 3

The admin SDK lets you get that data (metadata holds created & lastlogin). But I'm not sure if it is meant to be implemented into the App.

Solution 4

You can structure your rules like this

{ 
  rules: { 
    users: {

     uid_01: {
       //You can also do regular expression of timestamp and add it to ".validate" rule
       lastLogin: { ".validate": "newData.isString()"   }     
       createdOn: { ".write": "!data.exists()" }
      }
    }
  } 
}

Having these above rules will help you manage what you are asking for. "createdOn" will only let you write value once. Whenever you create new user just write the date to "createdOn" and keep updating "lastLogin" overtime they logIn!!

Share:
13,209
iOSGeek
Author by

iOSGeek

Updated on July 22, 2022

Comments

  • iOSGeek
    iOSGeek almost 2 years

    I just want to get the created date and the last sign in date of a user with firebase, my plan is to let user login with Facebook and then signup and add new information and save the user in the real time database with the created date and last login date, so how to keep the date un sync with the Firebase system after saving them, or should I just use those f FIRUser and do not save them in the real time database

    enter image description here

    Question 1: how to get the created and last login date from FIRUser like displayed in the dashboard (see attached image)

    Question 2: Once I get those dates I will save them in realtime database, so how to keep the last sign in date in sync ?

  • bojeil
    bojeil about 6 years
    The question was for the iOS SDK and not the JS one.
  • DavidTaubmann
    DavidTaubmann almost 5 years
    I believe the values have been renamed to lastSignInTime and creationTime
  • bojeil
    bojeil almost 5 years