Search code examples
firebasegoogle-cloud-firestorefirebase-authenticationswiftuiapple-sign-in

Cannot create document in Firestore when using Sign in with Apple


In my project I'm using a trigger to create a user document in Firestore when the user signs in. And this is great - everything works perfect for Google Sign-In and Facebook Login.

Here is this trigger:

    exports.createAccountDocument = functions.auth.user().onCreate(async user => {
      const { uid, displayName } = user
      const username = displayName;
      const email = user.email || user.providerData[0].email;
      const profileImageUrl = uid;
      const status = "active";
    
    const str = username;
    const qwery = [];
    for (let i = 0; i < str.length; ++i) {
        qwery[i] = str.substring(0, i + 1).replace(/\s/g, "").toLowerCase();
    }
      const keywords = qwery;
    
    
      const bio = "";
    
      return await admin
        .firestore()
        .collection("users")
        .doc(uid)
        .set({ bio, email, keywords, profileImageUrl, status, uid, username })
    })

For example - this is Facebook Login method:

    import SwiftUI
    import FBSDKLoginKit
    import Firebase
    
    struct FacebookAuthView: UIViewRepresentable {
    
        @Binding var showAnimation: Bool
        @Binding var showSheet: Bool
        
        init(showAnimation: Binding<Bool>, showSheet: Binding<Bool>) {
            self._showAnimation = showAnimation
            self._showSheet = showSheet
        }
        
        func makeCoordinator() -> FacebookAuthView.Coordinator {
            return FacebookAuthView.Coordinator(showAnimation: self.$showAnimation, showSheet: self.$showSheet)
        }
        
        class Coordinator: NSObject, LoginButtonDelegate {
            
            @Binding var showAnimation: Bool
            @Binding var showSheet: Bool
            
            init(showAnimation: Binding<Bool>, showSheet: Binding<Bool>) {
                self._showAnimation = showAnimation
                self._showSheet = showSheet
            }
            
            func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
                if let error = error {
                  print(error.localizedDescription)
                  return
                }
                guard let token = AccessToken.current else {
                    return
                }
                
                self.showAnimation = true
                self.showSheet = false
                
                let credential = FacebookAuthProvider.credential(withAccessToken: token.tokenString)
                Auth.auth().signIn(with: credential) { (authResult, error) in
                    if let error = error, (error as NSError).code == AuthErrorCode.credentialAlreadyInUse.rawValue {
                        Auth.auth().signIn(with: credential) { result, error in
                            // continue
                            print("signIn result: " + authResult!.user.email!)
                            if let token = firebaseRegistrationPushToken {
                                checkUserAuthSettings(pushToken: token)
                            }
                        }
                    } else {
                        // continue
                        print("Facebook Sign In")
                        if let token = firebaseRegistrationPushToken {
                            checkUserAuthSettings(pushToken: token)
                        }
                    }
    
                }
                
            }
            
            func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
                try! Auth.auth().signOut()
            }
        }
        
        func makeUIView(context: UIViewRepresentableContext<FacebookAuthView>) -> FBLoginButton {
            let view = FBLoginButton()
            view.permissions = ["email"]
            view.delegate = context.coordinator
            return view
        }
        
        func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<FacebookAuthView>) { }
    }

But when I try to create a document when user logs in using Swign in with Apple, this doesn't work. In the Firebase console under Firebase Authentication I can see new the user, but in Firestore, no document shows up at all.

Here is my Sign in with Apple method:

    import Foundation
    import SwiftUI
    import AuthenticationServices
    import CryptoKit
    import Firebase
    
    struct AppleAuthView: UIViewRepresentable {
    
        @Binding var showAnimation: Bool
        @Binding var showSheet: Bool
        
        init(showAnimation: Binding<Bool>, showSheet: Binding<Bool>) {
            self._showAnimation = showAnimation
            self._showSheet = showSheet
        }
        
        func makeCoordinator() -> AppleAuthView.Coordinator {
            return AppleAuthView.Coordinator(showAnimation: self.$showAnimation, showSheet: self.$showSheet)
        }
        
        class Coordinator: NSObject, ASAuthorizationControllerPresentationContextProviding, ASAuthorizationControllerDelegate {
            
            @Binding var showAnimation: Bool
            @Binding var showSheet: Bool
            fileprivate var currentNonce: String?
            
            init(showAnimation: Binding<Bool>, showSheet: Binding<Bool>) {
                self._showAnimation = showAnimation
                self._showSheet = showSheet
                super.init()
            }
            
            func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
                let viewController = UIApplication.shared.windows.last?.rootViewController
                return (viewController?.view.window!)!
            }
            
            func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
                if let appleIDCredential = authorization.credential 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 {
                        print("Unable to fetch identity token")
                        return
                    }
                    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 let error = error {
                            print(error.localizedDescription)
                            return
                        }
                        print("Apple Sign In")
                        if let token = firebaseRegistrationPushToken {
                            checkUserAuthSettings(pushToken: token)
                        }
                    }
                }
            }
            
            func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
                print("Sign in with Apple errored: \(error)")
            }
            
            @objc func startSignInWithAppleFlow() {
                let nonce = randomNonceString()
                currentNonce = nonce
                let appleIDProvider = ASAuthorizationAppleIDProvider()
                let request = appleIDProvider.createRequest()
                request.requestedScopes = [.fullName, .email]
                request.nonce = sha256(nonce)
                let authorizationController = ASAuthorizationController(authorizationRequests: [request])
                authorizationController.delegate = self
                authorizationController.presentationContextProvider = self
                authorizationController.performRequests()
            }
        
            private func randomNonceString(length: Int = 32) -> String {
                precondition(length > 0)
                let charset: Array<Character> = Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
                var result = ""
                var remainingLength = length
                while remainingLength > 0 {
                    let randoms: [UInt8] = (0 ..< 16).map { _ in
                        var random: UInt8 = 0
                        let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
                        if errorCode != errSecSuccess {
                            fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)")
                        }
                        return random
                    }
                    randoms.forEach { random in
                        if remainingLength == 0 {
                            return
                        }
                        if random < charset.count {
                            result.append(charset[Int(random)])
                            remainingLength -= 1
                        }
                    }
                }
                return result
            }
            
            @available(iOS 13, *)
            private func sha256(_ input: String) -> String {
                let inputData = Data(input.utf8)
                let hashedData = SHA256.hash(data: inputData)
                let hashString = hashedData.compactMap {
                    return String(format: "%02x", $0)
                }.joined()
                return hashString
            }
            
        }
        
        func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {
            let button = ASAuthorizationAppleIDButton(type: .signIn, style: .black)
            button.addTarget(context.coordinator,action: #selector(Coordinator.startSignInWithAppleFlow),for: .touchUpInside)
            return button
        }
    
        func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context: Context) {
        }
        
    }

I don't understand why the document cannot be created. In the console I can see nil.

Please help to fix this issue.

Updated. Code below - is a simple function to creating user document in firestore in my app

    func signup(username: String, email: String, password: String, imageData: Data, completed: @escaping(_ user: User) -> Void,  onError: @escaping(_ errorMessage: String) -> Void) {
        if !username.isEmpty && !email.isEmpty && !password.isEmpty && !imageData.isEmpty {
           AuthService.signupUser(username: username, email: email, password: password, imageData: imageData, onSuccess: completed, onError: onError)
        } else {
            
            if username == "" {
                errorString = "Please enter your name"
                onError(errorString)
//                showAlert = true
            }
            
            if email == "" {
                errorString = "Please enter yor valid email"
                onError(errorString)
//                showAlert = true
            }
            if password == "" {
                errorString = "Please create password"
                onError(errorString)
//                showAlert = true
            }
            if  image == Image(IMAGE_USER_PLACEHOLDER) {
                errorString = "Please upload your avatar"
                onError(errorString)
//                showAlert = true
            }
        }
    }

and here is AuthService.signupUser method

static func signupUser(username: String, email: String, password: String, imageData: Data, onSuccess: @escaping(_ user: User) -> Void, onError: @escaping(_ errorMessage: String) -> Void) {
        //Firebase.createAccount(username: username, email: email, password: password, imageData: imageData)
        Auth.auth().createUser(withEmail: email, password: password) { (authData, error) in
            if error != nil {
                print(error!.localizedDescription)
                onError(error!.localizedDescription)
                return
            }
            
            guard let userId = authData?.user.uid else { return }
            
            
            let storageAvatarUserId = Ref.STORAGE_AVATAR_USERID(userId: userId)
            let metadata = StorageMetadata()
            metadata.contentType = "image/jpg"
            
            StorageService.saveAvatar(userId: userId, username: username, email: email, imageData: imageData, metadata: metadata, storageAvatarRef: storageAvatarUserId, onSuccess: onSuccess, onError: onError)
            
        }
    }

I know that apple sign in cannot take users image & it's ok - my cloud trigger fill this field in document users uid & in my app if user avatar = nil - it's takes universal clipart, It's ok, newer mind. And this is User file

import Foundation

struct User: Encodable, Decodable {
    var uid: String
    var email: String
    var profileImageUrl: String
    var username: String
    var bio: String
    var keywords: [String]
    var status: String?
}

my security rules in firebase are simple, here they are

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

Solution

  • This issue is - that Apple don’t get username. Issue is solved.