d0xi45 d0xi45 - 1 month ago 29
Swift Question

Type "user?" had no member UID (Swift 3, Firebase)

I am setting up a login page with Firebase and I've run into an error where uid isn't getting recognized. Here's my code:

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (FIRUser,error) in

if error != nil {
print(error)
return

}

guard let uid = user?.uid else {
return
}


I also can't command-click into FIRUser, however if I get rid of the guard statement, authentication works perfectly fine.

I have Firebase and FirebaseDatabase imported (can't import FirebaseAuth, it's crossed out), so it's not that.

Any idea why I can't access FIRUser and use uid?

Answer

You need to create the variable user inside your closure. Rewrite your function to this instead.

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user: FIRUser?, error: Error?) in

    if error != nil {
        print(error)
        return

    }

    guard let uid = user?.uid else {
        return
    }