title: sign in with apple to firebase unable to register at firebase
body: <p>I'm trying to build registration with Apple to firebase.
  I successful sign up to Apple but that gives me the next error</p>
  <pre><code>Optional(__C.FIRAuthErrorCode)
  Optional(Error Domain=FIR
  AuthErrorDomain Code=17094 &quot;Nonce is missing in the request.&quot; UserInfo= 
  {NSLocalizedDescription=Nonce is missing in the request., 
  FIRAuthErrorUserInfoNameKey=ERROR_MISSING_OR_INVALID_NONCE})
  </code></pre>
  <p>That's my code</p>
  <pre><code>import UIKit
  import Foundation
  import AuthenticationServices
  import Firebase
  import CryptoKit
  
  class firstCV: UIViewController {
     
      fileprivate var currentNonce: String?
  
      override func viewDidLoad() {
          super.viewDidLoad()
          setUpView()
          
          
          
          navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
          
      
      }
      
      static func randomNonceString(length: Int = 32) -&gt; String {
          precondition(length &gt; 0)
          let charset: Array&lt;Character&gt; =
              Array(&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._&quot;)
          var result = &quot;&quot;
          var remainingLength = length
          
          while remainingLength &gt; 0 {
              let randoms: [UInt8] = (0 ..&lt; 16).map { _ in
                  var random: UInt8 = 0
                  let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &amp;random)
                  if errorCode != errSecSuccess {
                      fatalError(&quot;Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)&quot;)
                  }
                  return random
              }
              
              randoms.forEach { random in
                  if length == 0 {
                      return
                  }
                  
                  if random &lt; charset.count {
                      result.append(charset[Int(random)])
                      remainingLength -= 1
                  }
              }
          }
          
          return result
      }
      
      @available(iOS 13.0, *)
      override func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
          guard let nonce = currentNonce else {
      
            fatalError(&quot;Invalid state: A login callback was received, but no login request was sent.&quot;)
          }
          guard let appleIDToken = appleIDCredential.identityToken else {
            print(&quot;Unable to fetch identity token&quot;)
            return
          }
          guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
            print(&quot;Unable to serialize token string from data: \(appleIDToken.debugDescription)&quot;)
            return
          }
  
          // Initialize a Firebase credential.
          
          let credential = OAuthProvider.credential(withProviderID: &quot;apple.com&quot;,
                                                    idToken: idTokenString,
                                                    accessToken: nonce)
          // Sign in with Firebase.
          Auth.auth().signIn(with: credential) { (authResult, error) in
              if (error != nil) {
                  
                  
                  
                  let castedError = error! as NSError
                  let firebaseError = AuthErrorCode(rawValue: castedError.code)
                  
                 
              // Error. If error.code == .MissingOrInvalidNonce, make sure
              // you're sending the SHA256-hashed nonce as a hex string with
              // your request to Apple.
  
              return
            }
            let storyboard = UIStoryboard(name: &quot;Main&quot;, bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: &quot;ageVC&quot;)
            self.present(controller, animated: true, completion: nil)
            if let vc = UIStoryboard(name: &quot;Main&quot;, bundle: nil).instantiateViewController(withIdentifier: &quot;ageVC&quot;) as? ageVC
            {
              self.present(vc, animated: true, completion: nil)
            }
          }
        }
      }
  
      @available(iOS 13.0, *)
      override func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        // Handle error.
        print(&quot;Sign in with Apple errored: \(error)&quot;)
      }
  
      
      @available(iOS 13, *)
      static func sha256(_ input: String) -&gt; String {
        let inputData = Data(input.utf8)
        let hashedData = SHA256.hash(data: inputData)
        let hashString = hashedData.compactMap {
          return String(format: &quot;%02x&quot;, $0)
        }.joined()
  
        return hashString
      }
      
      func setUpView(){
          
          if #available(iOS 13.0, *) {
              let appleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
               appleButton.translatesAutoresizingMaskIntoConstraints = false
              
              appleButton.addTarget(self, action: #selector(didTapAppleButton), for: .touchUpInside)
              
              view.addSubview(appleButton)
              NSLayoutConstraint.activate([appleButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -30),
                                           appleButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 45),
                                           appleButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -45),
                                           appleButton.heightAnchor.constraint(equalToConstant: 40)])
              
              view.bringSubviewToFront(appleButton)
              
          } else {
              // Fallback on earlier versions
          }
      }
      
      
      
      @available(iOS 13.0, *)
      @objc
      func didTapAppleButton(){
          let nonce = firstCV.randomNonceString()
          currentNonce = nonce
          let appleIDProvider = ASAuthorizationAppleIDProvider()
          let request = appleIDProvider.createRequest()
          request.requestedScopes = [.email]
          request.nonce = firstCV.sha256(nonce)
  
          let controller = ASAuthorizationController(authorizationRequests: [request])
          
          controller.delegate = self
          controller.presentationContextProvider = self
          controller.performRequests()
      }
  
  }
  
  
  extension UIViewController: ASAuthorizationControllerDelegate{
      
      @available(iOS 13.0, *)
      public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authrezation: ASAuthorization){
  
      }
      
      @available(iOS 13.0, *)
      public func authorizationController(controller: ASAuthorizationController, didCompleteWithError: Error){
          
      }
      
  }
  extension UIViewController: ASAuthorizationControllerPresentationContextProviding{
      @available(iOS 13.0, *)
      public func presentationAnchor(for controller: ASAuthorizationController) -&gt; ASPresentationAnchor {
           return view.window!
      }
  }
  </code></pre>