Agung
Agung

Reputation: 13803

how to check if user has already signed up or not in firebase via google account in swift

I want to do an action for user that login for the first time (signed up) using google account and another action if the user has already login before.

if some user has been and still logged in using their Google account, we can use this lines of code

Auth.auth().addStateDidChangeListener { (auth, user) in
    if user == nil {
        self.goToLoginVC()
    }
}

but how can I differentiate if a user login for the first time (signed up) or not ? is there any particular method to accomplish this? Thanks

Upvotes: 3

Views: 1450

Answers (1)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16446

You can use UserDefault to check the same. Just maintain the flag to check

like

    GIDSignIn.sharedInstance().signInSilently()


   Auth.auth().addStateDidChangeListener { (auth, user) in
        if user != nil {
             if UserDefaults.standard.string(forKey: "uid") != nil && Auth.auth().currentUser != nil {
              //User was already logged in 
               }

             UserDefaults.standard.setValue(user?.uid, forKeyPath: "uid")

            self.performSegue(withIdentifier: "CurrentlyLoggedIn", sender: nil)

        }
    }

Upvotes: 3

Related Questions