I have written an app that sign's a user in by using the google signin package and firebase. I would like to write some functionality which would be
if the user has signed into the app using google previously
then take them straight to the main ViewController
otherwise they must be signing in for the first time
then bring to a view controller to create a profile
I essentially just want a condition which returns true/false if the user has signed into the app previously, and returns the opposite if this is a new user.
My View Controller
This is my signin view controller, which is the first view visible in my app. Right now It's only set up to print "new user", if the user has never logged into the app, and "old user" if this is a returning user.
@objc(SignInViewController)
class SignInViewController: UIViewController, GIDSignInUIDelegate{
@IBOutlet var signInButton: GIDSignInButton!
var handle: AuthStateDidChangeListenerHandle?
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
handle = Auth.auth().addStateDidChangeListener() { (auth, user) in
if user != nil {
MeasurementHelper.sendLoginEvent()
dump(UserDefaults.standard)
//This is the part which I need help with/////////////////////
if this user has logged in previously
print("old user")
self.performSegue(withIdentifier: Constants.Segues.SignInToFp, sender: nil)
}else{
print("new user")
self.performSegue(withIdentifier: Constants.Segues.SignInToFp, sender: nil)
}
////////////////////////////////////////////////////////////
}
}
}
deinit {
if let handle = handle {
Auth.auth().removeStateDidChangeListener(handle)
}
}
Other solutions which didn't work for me
This solution looks promising but it only explains how to do it with Facebook and not google.
With this answer the nested if statment if UserDefaults.standard.string(forKey: "uid") != nil && Auth.auth().currentUser != nil { always returns false. The first condition is the one that always return's false, but the second condition is the one that always return's true. (This happens with accounts where I login multiple times, and brand new accounts). When I dump(UserDefaults.standard) I receive
- <NSUserDefaults: 0x2825dd140> #0
- super: NSObject
The solution which includes checking the last login date with the creation date(newUserRref?.creationDate?.timeIntervalSince1970 == newUserRref?.lastSignInDate?.timeIntervalSince1970 ) always returned false for me, so it returned that no user was ever a new user. There's some other solutions on that post that I don't fully understand
The documentation doesn't really explain how to check if this is a first sign in, I can only see it saying how to create a new user or sign in an existing user, but I don't know how I would know when to do what.
So this worked at first, but then stopped working, but
newUserRref?.creationDate?.timeIntervalSince1970andnewUserRref?.lastSignInDate?.timeIntervalSince197are almost identical but they differ at the about the 3rd decimal place(One will be Optional(1553623223.055) and the other will be Optional(1553623223.057))I had to modify the solution from the third posted link to include an extention to the double type, which I place directly at the bottom of my SignInViewControllerClass.
And then the nested if statement I used to check if the user is new or old is