Firebase Authentication

I added firebase auth to my app, however, I don't think the code is right. I just wanted someone to double check it for me. I have phone auth, and email/password auth. Here is the code:


    //MARK: Phone Verification
    //Sends code to mobile device
    @IBAction func sendCode(_ sender: Any) {
        PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber.text!, uiDelegate: nil) { (verificationID, error) in
            if let error = error {
                print(error as Any)
                return
            }
            // Sign in using the verificationID and the code sent to the user
            // ...
            UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
            
        }
    }
    
    //Checks verification code
    @IBAction func enterverificationCode(_ sender: Any) {
        let verificationID = UserDefaults.standard.string(forKey: "authVerificationID")
        let credential = PhoneAuthProvider.provider().credential(
            withVerificationID: verificationID!,
            verificationCode: verificationCode.text!)
        Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
            if error != nil {
                // ...
                return
            }
            // User is signed in
            // ...
            if Auth.auth().currentUser != nil {
                // User is signed in.
                // ...
            } else {
                // No user is signed in.
                // ...
            }
        }
    }
    
    //MARK: Email/Password Verification
    //Creates account for email/password verification
    @IBAction func createEmailPasswordAccount(_ sender: Any) {
        Auth.auth().createUser(withEmail: email1.text!, password: password1.text!) { (authResult, error) in
            // ...
        }
    }
    
    @IBAction func signInEmailPasswordAccount(_ sender: Any) {
        Auth.auth().signIn(withEmail: email1.text!, password: password1.text!) { (user, error) in
            // ...
            if Auth.auth().currentUser != nil {
                // User is signed in.
                // ...
            } else {
                // No user is signed in.
                // ...
            }
        }
    }
    
    @IBAction func signOut(_ sender: Any) {
        let firebaseAuth = Auth.auth()
        do {
            try firebaseAuth.signOut()
        } catch let signOutError as NSError {
            print ("Error signing out: %@", signOutError)
        }
    }

Replies

Where have you a problem ?


I noted that in some cases, you need to use


Firebase.auth().signIn(withEmail: email1.text!, password: password1.text!) { (user, error) in {   }


instead of


Auth.auth().signIn(withEmail: email1.text!, password: password1.text!) { (user, error) in

I believe that those are the same things. Something else must be different.

Not really useful to reactivate a thread that is 2 years old unless you have a definite answer.

That's why it is also a good practice for the OP author to close the thread.

If you have a problem, please start a new thread, possibly with a link to the old one.

PS: Firebase questions would better be asked to Firebase.