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

好久不见. 提交于 2019-12-30 09:58:06

问题


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

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 ?


回答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




回答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



回答3:


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!!




回答4:


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.



来源:https://stackoverflow.com/questions/38174178/get-created-date-and-last-login-date-from-firuser-with-firebase-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!