0

I implemented the login to the application using the Apple, at the first login, the visibility of the mail and the name are indicated. I need to use this name after logging in. But after the user logs in, the Firestore Database section is empty. How do I add the name data from the registration there?

SignInWithAppleButton(
    onRequest: { request in
        let nonce = randomNonceString()
        currentNonce = nonce
        request.requestedScopes = [.fullName, .email]
        request.nonce = sha256(nonce)
    },
    onCompletion: { result in

        switch result {
        case .success(let authResults):
            switch authResults.credential {
            case let appleIDCredential as ASAuthorizationAppleIDCredential:
                
                guard let nonce = currentNonce else {
                    fatalError("Invalid state: A login callback was received, but no login request was sent.")
                }
                guard let appleIDToken = appleIDCredential.identityToken else {
                    fatalError("Invalid state: A login callback was received, but no login request was sent.")
                }
                guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
                    print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
                    return
                }
                
                let credential = OAuthProvider.credential(withProviderID: "apple.com",idToken: idTokenString,rawNonce: nonce)
                Auth.auth().signIn(with: credential) { (authResult, error) in
                    if (error != nil) {
                        // Error. If error.code == .MissingOrInvalidNonce, make sure
                        // you're sending the SHA256-hashed nonce as a hex string with
                        // your request to Apple.
                        print(error?.localizedDescription as Any)
                        return
                    }
                    print("signed in")
                    self.userAuth.login()
                }
                
                print("\(String(describing: Auth.auth().currentUser?.uid))")
            default:
                break
                
            }
        default:
            break
        }
    }
)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Does this answer your question? [Returning data from async call in Swift function](https://stackoverflow.com/questions/25203556/returning-data-from-async-call-in-swift-function) – timbre timbre Dec 07 '21 at 17:51
  • What's being asked is unclear. Does the code in the question work or not? If if works, do you have some code that isn't working? If it doesn't work where does it go wrong? We don't know what `Firestore Database section is empty` means - what section.. Section of code? Or In Firebase? What Firebase product are you using? – Jay Dec 07 '21 at 18:41
  • @Jay from the code, OP is trying to use `Auth.auth().currentUser?.uid` outside of callback (immediately after callback). Classic case of someone not knowing how to work with asynch calls in swift. – timbre timbre Dec 07 '21 at 19:30
  • @KirilS. Wow - you are so right! I usually notice asynchronous issues like that, but duh on me for not scrolling down. lol. Great catch! The print statement will not print anything because that code executes before the code in the closure. However, this `self.userAuth.login()` code will still execute if the auth is successful. OP: The rest of the question needs clarification. – Jay Dec 07 '21 at 20:12
  • I'm sorry if I'm not clear, I'm a beginner. The Name and Email that I requested from the user at the time of registration should have been reflected in Firebase in the Firestore Database section? I need it to use this data (email name) in the future in the application itself. – Elchin Aliev Dec 08 '21 at 01:59
  • The code works, the user registers successfully, the login data is reflected in Firebase Authentication. I can't figure out how to use the data further – Elchin Aliev Dec 08 '21 at 02:06
  • There is nothing in the code that writes any data to the "Firebase database". e.g. there is no 'Firebase Database section' in the code in the question. Keep in mind that Firebase is a suite of services that includes Authentication, Storage, two databases etc. For database work, there is Firestore and the Realtime Database - but you're not using either. When replying to a comment here on SO, reply using the at symbol with the users name so it will get their attention, like @Jay. – Jay Dec 08 '21 at 17:55

0 Answers0