简体   繁体   中英

Firebase Convert Anonymous to Permanent Account Error

I have the user already Signed in Anonymously in my app through the following code:

        Auth.auth().signInAnonymously { (result, error) in
            if let error = error {
                Auth.auth().handleFireAuthError(error: error, vc: self)
                debugPrint(error)
            }
        }

Then at one point I require the user to sign up and I am using the following code to link the Anonymous account to a permanent account:

    guard let email = userEmailTextField.text, email.isNotEmpty,
        let password = userPasswordTextField.text, password.isNotEmpty else {
            simpleAlert(title: "Error", msg: "Please fill out all fields.")
            return
    }

    guard let confirmPass = userRepeatPasswordTextField.text , confirmPass == password else{
        simpleAlert(title: "Error", msg: "Passwords do not match.")
        return
    }

    guard let authUser = Auth.auth().currentUser else {
        return
    }

    let credential = EmailAuthProvider.credential(withEmail: email, link: password)

    authUser.link(with: credential) { (result, error) in

        if let error = error {
            debugPrint(error)
            Auth.auth().handleFireAuthError(error: error, vc: self)
            self.activityIndicator.stopAnimating()
            return
        }

        guard let fireUser = result?.user else {return}
        let artUser = User.init(id: fireUser.uid, email: email , stripeId:"")
        //Upload to Firestore
        self.createFirestoreUser(user: artUser)
    }

But I get an error here. I put break point and this is where the execution stop authUser.link(with: credential) { (result, error) in

This is the error I am getting:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSURLComponents initWithString:]: nil URLString parameter'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23e39f0e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50ad79b2 objc_exception_throw + 48
    2   Foundation                          0x00007fff259659ca -[__NSConcreteURLComponents URL] + 0
    3   Foundation                          0x00007fff259673a8 +[NSURLComponents componentsWithString:] + 33
    4   Silobee                             0x000000010b144c52 +[FIRAuthWebUtils parseURL:] + 98
    5   Silobee                             0x000000010b16c638 __56-[FIRUser linkAndRetrieveDataWithCredential:completion:]_block_invoke_3 + 440
    6   Silobee                             0x000000010b16b084 __51-[FIRUser internalGetTokenForcingRefresh:callback:]_block_invoke + 340
    7   Silobee                             0x000000010b15df8c __65-[FIRSecureTokenService fetchAccessTokenForcingRefresh:callback:]_block_invoke + 156
    8   Silobee                             0x000000010b13fa7b __38-[FIRAuthSerialTaskQueue enqueueTask:]_block_invoke + 155
    9   libdispatch.dylib                   0x0000000110850f11 _dispatch_call_block_and_release + 12
    10  libdispatch.dylib                   0x0000000110851e8e _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x00000001108586fd _dispatch_lane_serial_drain + 788
    12  libdispatch.dylib                   0x00000001108592c5 _dispatch_lane_invoke + 476
    13  libdispatch.dylib                   0x000000011085851c _dispatch_lane_serial_drain + 307
    14  libdispatch.dylib                   0x000000011085928f _dispatch_lane_invoke + 422
    15  libdispatch.dylib                   0x0000000110864b65 _dispatch_workloop_worker_thread + 719
    16  libsystem_pthread.dylib             0x00007fff51b37a3d _pthread_wqthread + 290
    17  libsystem_pthread.dylib             0x00007fff51b36b77 start_wqthread + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Just saw where my problem is: Instead of this:

let credential = EmailAuthProvider.credential(withEmail: email, link: password)

I had to write this:

let credential = EmailAuthProvider.credential(withEmail: email, password: password)

One small details and stoped my code from working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM