Internal Server Error when i try apple login with Firebase

  Kiến thức lập trình
func appleLogin(){
    let nonce = String().randomNonceString()
    currentNonce = nonce

    let appleIDProvider = ASAuthorizationAppleIDProvider()
    let request = appleIDProvider.createRequest()
    request.requestedScopes = [.fullName, .email]
    request.nonce = String().sha256(nonce)
  
    let authorizationController = ASAuthorizationController(authorizationRequests: [request])
    authorizationController.delegate = self
    authorizationController.presentationContextProvider = self
    authorizationController.performRequests()
}

func authorizationController(controller: ASAuthorizationController,
                             didCompleteWithAuthorization authorization: ASAuthorization) {
    signupViewModel.appleLogin(authorization: authorization, currentNonce: currentNonce) {
        self.loginDidSucceed {
            UIApplication.shared.windows.first?.isUserInteractionEnabled = true

            self.signupViewModel.searchUID()
        }
    }
}

func appleLogin(authorization: ASAuthorization,
                currentNonce: String?, completion: @escaping() -> Void){
    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
        }
    
        if let authorizationCode = appleIDCredential.authorizationCode,
           let codeString = String(data: authorizationCode, encoding: .utf8) {
            print(codeString)
            let url = URL(string: "https://MY-URL?code=(codeString)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "https://apple.com")!
            let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
                if let data = data {
                    let refreshToken = String(data: data, encoding: .utf8) ?? ""
                    print(refreshToken)
                    UserDefaults.standard.set(refreshToken, forKey: "refreshToken")
                    UserDefaults.standard.synchronize()
                }
            }
            task.resume()
        }
      
        // Initialize a Firebase credential, including the user's full name.
        let credential = OAuthProvider.credential(withProviderID: "apple.com",
                                                  idToken: idTokenString,
                                                  rawNonce: nonce)
      
        // Sign in with Firebase.
        Auth.auth().signIn(with: credential) { authResult, error in
            if let error = error {
                print("Error Apple sign in: (error.localizedDescription)")
                return
            }
            completion()
        }
    }
}

`The code above is the Apple login code. In MY-URL, I use the URL saved in Firebase functions. However, when trying to issue a refresh token with this code, Internal Server Error
happens. Looking at the Firebase logs, it looks like this:

"AxiosError: Request failed with status code 400
at settle (/workspace/node_modules/axios/dist/node/axios.cjs:1966:12)
at IncomingMessage.handleStreamEnd (/workspace/node_modules/axios/dist/node/axios.cjs:3065:11)
at IncomingMessage.emit (node:events:529:35)
at IncomingMessage.emit (node:domain:552:15)
at endReadableNT (node:internal/streams/readable:1400:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
at Axios.request (/workspace/node_modules/axios/dist/node/axios.cjs:3876:41)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"

i don’t know what i have to do to solve this problem
i tried to re-upload Firebase functions and correct index.js
`

New contributor

rrheon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT