Mar Dillo
Mar Dillo

Reputation: 121

"Value of type 'AuthDataResult' has no member 'uid'" error

Can someone help me about this issue? The first error was "value of type 'authdataresult' has no member 'uid'".

Auth.auth()?.signIn(withEmail: email, password: password, completion:

So I removed the question mark to resolve the problem and it worked! But after that, another error came and it said "Value of type 'AuthDataResult' has no member 'uid'". I know, it's irritating.

self.userUid = user.uid

And oh, there's another error and it's been in my project for a very long time so now i'm ignoring it but then if someone knows how to fix it please let me know. The error is "only instance methods can be declared @ibaction".

@IBAction func signInTapped(_ sender: Any) {

I don't know why but maybe the 3 errors are connected. I don't know. Help. So now I'm stuck again. Here is the whole messed up coding stuff.

import UIKit
import Firebase

class ViewController: UIViewController {
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!

    var userUid: String!

    func goToCreateUserVC(){
        performSegue(withIdentifier: "SignUp", sender: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "SignUp" {
            if let destination = segue.destination as? userVC {
                if userUid != nil {
                    destination.userUid = userUid
                }
                if emailField.text != nil {
                    destination.emailField = emailField.text
                }
                if passwordField.text != nil {
                    destination.passwordField = passwordField.text
                }
            }
        }

        @IBAction func signInTapped(_ sender: Any) {
            if let email = emailField.text, let password = passwordField.text {
                Auth.auth().signIn(withEmail: email, password: password, completion: { (user,error) in
                    if error == nil {
                        if let user = user {
                            self.userUid = user.uid
                            self.goToCreateUserVC()
                        }
                    } else {
                        self.goToCreateUserVC()
                    }
                });
            }
        }
    }
}

Like I swear, I tried to fix it and then 9 errors just popped out. By the way, I'm using Xcode 9, Swift 4, and my Cocoapods are up-to-date. To those all around people who edit, comment, or judge questions, please this is not a duplicate question. They are 2 different errors.

Upvotes: 10

Views: 7285

Answers (3)

mila kohen
mila kohen

Reputation: 15

guard let uid = user?.user.uid else {return}
let ref = Database.database().reference(fromURL: "")
let usersReference = ref.child("users").child(uid)

Upvotes: 0

Lance Samaria
Lance Samaria

Reputation: 19582

The new Auth.auth().signIn and Auth.auth().createUser methods use a datatype of AuthDataResult? instead of User?

The old callback was user of type: User?

Auth.auth().signIn(withEmail: email, password: password, completion: { (user: User?, error) in

You would access properties directly on the callback user object such as:

if error == nil {
    if let user = user {
        self.userUid = user.uid
        // user.email for the email address in firebase
    }
}

The new callback is authDataResult of type: AuthDataResult?

Auth.auth().signIn(withEmail: email, password: password, completion: { (authDataResult: AuthDataResult?, error) in

Now you access properties directly on the callback authDataResult object such as:

if error == nil {
    if let authDataResult = authDataResult {
        self.userUid = authDataResult.user.uid
        // authDataResult.user.email for the email address in firebase
    }
}

Basically the user object from the old way is now a property on the authDataResult object and you can get the uid through there:

authDataResult.user.uid

Firebase's info about AuthDataResult

Upvotes: 4

Sami Sharafeddine
Sami Sharafeddine

Reputation: 410

It seems that Google has made some changes to Firebase Auth with the 5.0.0 update. This worked for me:

self.userUid = user.user.uid

I'm not sure what was the point of the change but it still works exactly the same.

Upvotes: 30

Related Questions