Get Created Date and Last Login Date from Firuser with Firebase 3

Extract Created date and last login from firebase authentication using Python

It looks like the ExportedUsers result for list_users does not include the metadata for the users. In that case you'll need to call get_user(...) for each UID in the result to get the full UserRecord, and then find the timestamps in the metadata.

how to store date in the firebase with type 'timestamp' instead of 'map' from reactjs

I think you will need to do something like this to save it as timestamp on firebase.

firebase.firestore.Timestamp.fromDate(date).toDate());

Swift 3.0, Firebase: How to get last added record

You can store your date in the form YYYYMMDD, it is then sortable in date order. For example, represent November 2 2017 as 20171102.

When you set up your query, order it by key and then limit it to the last value.

Get Date as string from Firebase convert to date and compare that date with current to find difference in minutes

Get the date from firebase in String

let dateString = "18-11-2017 10:41 AM"

Simple extention to convert String to Date with your date format

extension String{
var date : Date?{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy h:mm a"
return dateFormatter.date(from: self)
}
}

Compare minutes from 2 dates

extension Date {
func minutes(from date: Date) -> Int {
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}
}

Compare your date with current date

if let date = dateString.date{
let minutes = Date().minutes(from: date)
}

Create a profile after user signs in Firebase and SwiftUI

To detect is a signed-in user is new, you can compare lastSignInDate and creationDate in the UserMetadata object.

Also see:

  • Get Created date and last login date from FIRUser with Firebase 3
  • iOS/Swift/Firebase Authentication: Help Needed on Accessing "isNewUser", which shows the isNewUser that you can also sometimes use (depending on your implementation).

Sort Firebase Data with queryOrdered by date

I think what you're missing here is the sort function, Please try the code below and let me know what are the results:

DataService.ds.REF_INCOMES.queryOrdered(byChild: "date").observe(.value, with: { (snapshot) in

guard let usersSnapshot = snapshot.value as? [String:NSObject] else{

return
}

let users = usersSnapshot["users"] as! [String:AnyObject]
let sorted = users.sorted{($0.0.value["date"] as! Double) > ($0.1.value["date"] as! Double)}

print(sorted) // It should be: New Year Now, New Year, Last Year
})

Swift iOS Firebase -If the FirebaseAuth.FIRUser Object isn't nil how is it possible for the .email on it to return nil?

After having a convo with @Benjamin Jimenez and per @kbunarjo suggestions I did some thinking and I realized that Firebase also has a Phone Sign-In Method. If it's enabled and a user chooses that as their sign-in method then the .email address would be nil because there wouldn't be an email address used to create an account.

Sample Image

For my situation since I'm using the Email/Password Sign-In Method then the .email should not be nil because it is the only way to create an account. But just as User can come back as nil in the completion handler (even with the correct email and password) maybe it’s possible that the email address can also come back as nil since it’s an Optional.

That being said the safest method to use would be another if-let to check to see if the user.email isn't nil and if for some strange reason that it is then use the email address that was entered into the emailTextField as an alternative. The email address entered into the emailTextField and password combo has to be correct otherwise the user won't get authenticated into Firebase and error != nil.

Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: {
(user, error) in

// if the email address and/or password are incorrect then error != nil and the code below this wont run
if error != nil { return }

if let user = user{

let sharedInstance = FirebaseSingleton.sharedInstance
sharedInstance.currentUser = user
sharedInstance.currentUserID = user.uid
sharedInstance.currentUserPhotoUrl = user.photoURL
sharedInstance.currentUserDisplayName = user.displayName

// instead of the guard statement I use another ‘if-let’ if for some reason .email is nil
if let currentUserEmail = user.email {
sharedInstance.currentUserEmail = currentUserEmail
}else{
sharedInstance.currentUserEmail = self.emailTextField.text! // the email address entered into here has to be valid otherwise it would've never made it to this point
}
}else{
return
}
})

Use the same exact code for the callback if Creating an Account

As @rMickeyD noted the Facebook Sign-In Method won’t use the email address either. I’ve never used it so I didn’t include it. Basically if none of the other Sign-In Methods don’t use your email address then user.email will be nil. Even if though I have Anonymous Sign-In as an option the method to use Anonymous Sign-In doesn’t require an email address nor a password so it’s understood that email will be nil.

You should check the docs to see if they require the email or not for all the other Sign-In Methods but the Phone number doesn’t need it.



Related Topics



Leave a reply



Submit