Login Fails on Simulator

I have an app that uses App Services Authentication service, and Sign In with Apple.

When testing on my device, it logs in as expected. When I try to login with the Simulator, login fails with a JSON parsing error. Stepping trough the code, I found that the response body in realm-core/src/realm/object-store/sync/app.cpp:607 prints as a HTML page when logging in trough the Simulator.

(lldb) po response.body
"<!DOCTYPE html>\n<html>\n  <head>\n      <title>App Services</title>\n....

On my device, response.body is a JSON object as expected.

What’s going on with this? It reproduces every time after re-install, product clean, depenency updates, and using emailPassword auth.

Are you stating the issue occurs with both?

Did you try deleting the CoreSimulator directory so a new one is generated?

Also, can you share your authentication code, and what version of Realm are you using (if you use a podfile, that may be helpful as well)

1 Like

The exact error is:
Error Domain=io.realm.app Code=-1 “[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: ‘<’” UserInfo={Error Code=-1, Server Log URL=, Error Name=MalformedJson, NSLocalizedDescription=[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: ‘<’}

Hi Jay! Thanks for responding, and apologies for leaving you hanging.

I worked around this by just testing on Mac (Designed for iPad) and physical devices instead. But today, this happened on a physical device as well.

It crashed iPhone 11 Pro if that’s helpful. It DOES work on an iPhone 14 Pro Max, and a iPhone 13.

So let’s resolve this :smiley:

The issue happens with both SIwA and email/password auth, yes. It seems to stem from an invalid response from Atlas Auth where a HTML document is returned in place of the intended auth response.

Printing the response at that function yields this, rather than JSON.

(std::string) body = "<!DOCTYPE html>\n<html>\n  <head>\n      <title>App Services</title>\n      <link rel=\"shortcut icon\" href=\"/static/favicon.ico\" type=\"image/x-icon\" />\n      <style>\n        #app { display: none; }\n      </style>\n      <script>\n        var settings = {\"accountUIBaseUrl\":\"https://account.mongodb.com\",\"adminUrl\":\"https://realm.mongodb.com\",\"apiUrl\":\"https://realm.mongodb.com\",\"bugsnagToken\":\"d93dd442ef3db183db76cc9ded3bc109\",\"chartsUIBaseUrl\":\"https://charts.mongodb.com\",\"cloudUIBaseUrl\":\"https://cloud.mongodb.com\",\"endpointAPIUrl\":\"https://data.mongodb-api.com\",\"endpointAPIUrlsByProviderRegion\":{\"aws-ap-south-1\":\"https://ap-south-1.aws.data.mongodb-api.com\",\"aws-ap-southeast-1\":\"https://ap-southeast-1.aws.data.mongodb-api.com\",\"aws-ap-southeast-2\":\"https://ap-southeast-2.aws.data.mongodb-api.com\",\"aws-eu-central-1\":\"https://eu-central-1.aws.data.mongodb-api.com\",\"aws-eu-west-1\":\"https://eu-west-1.aws.data.mongodb-api.com\",\"aws-eu-west-2\":\"https://eu-west-2.aws.data.mongodb-api.com\",\"aws-sa-east-1\":\"https://sa-east"...

This is in response to this login code:

    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        guard
            let credential = authorization.credential as? ASAuthorizationAppleIDCredential,
            let token = credential.identityToken,
            let tokenString = String(data: token, encoding: .utf8)
        else {
            let alert = UIAlertController(title: "Login Error", message: "No Credential", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel))
            present(alert, animated: true)
            return
        }

        Task { @MainActor in
            do {
                let user = try await realmApp.login(credentials: .apple(idToken: tokenString))
                delegate?.loginViewControllerDidLogin(with: user, name: credential.fullName)
            } catch {
                print(String(describing: error))
                presentError(title: "Login Error", error: error)
            }
        }
    }

where i’ve also verified that the token looks real:

"eyJraWQiOiJXNldjT0tCIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLmdldHdlYXJpdC53ZWFyaXQiLCJleHAiOjE2OTA1NzM4NDEsImlhdCI6MTY5MDQ4NzQ0MSwic3ViIjoiMDAwMTQ2Ljg4NTEzNGFlYzIyZjQyOTRiMWMzYTIzNTU1YzU5NTE5LjAzMzMiLCJjX2hhc2giOiIwY0QxZElMX1RnLUN0cHpzMnZiSmx3IiwiZW1haWwiOiJvc2Nhci5hcGVsYW5kQGljbG91ZC5jb20iLCJlbWFpbF92ZXJpZmllZCI6InRydWUiLCJhdXRoX3RpbWUiOjE2OTA0ODc0NDEsIm5vbmNlX3N1cHBvcnRlZCI6dHJ1ZX0.hMjobLqtaBz8bqxVqKisW4UH56mcRI6z7FhxCifwGweN23nNMIxCSyKfUBrA_m2YMZ06JWJIYK5ZTDvziD6dIHQ-l-WLnhcPef715RUGzad-sSGhIvE4-_9V-rDoWYYnmZq2UfC9VCfBDIaeJCP_rFw4oNHu2E37sm5I7VS1u1zp110RkAKYp8ezL2UktB0NkBYAzjRw3czqvZSi9HJLhrNXHSNd2OQSMUfHdELhq9touZEvvs0Km6wL9zF3P4rjTknVP0tsp-tkS890NDeAYeV6nCJADCA3U2sfJrp8qMseAaqJwKtFAG-daLoFX2QdBZbb14gQEcGXsFBF54yMNw"

I’m on the latest version of Realm.

I have now also verified by reproducing in a fresh and extremely minimal iOS project. Same error. Error has to be server-side (or something really obvious I’m missing in the pasted code).

This certainly could be a server issue; however there’s a lot of unknowns in your code that could be either the cause or a contributing factor. Eliminating as many variables as possible may lead to more info.

One thing to try is to see if commenting out your authentication code and using the exact code supplied in the Apple User Auth Guide makes a difference. That will eliminate that section of code.

From there, working backwards to see if code called prior to that may be delivering wrong data. Reviewing Sign In With Apple is a good step.

One other question is that does the crash occur with both a fresh user, who has not signed in before as well as one that has signed in on the device previously? In the delegate, are you creating an account in your system using the data contained in the user identifier (per the Apple guide)?

Also, cross post to this StackOverflow post in case someone produces an answer there.

I messed about with the process, and found the culprit.

I had specified a baseURL in my RLMApp init…

let realmApp = App(
    id: "$id",
    configuration: AppConfiguration(
        // baseURL: "https://realm.mongodb.com/groups/#/apps/#",
        localAppName: Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String,
        localAppVersion: Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String
    )
)

Simply commenting baseURL away fixed the auth problem, without appearing to cause other issues.

I had added that because I setup Realm with some doc suggesting to use a configuration plist which contained a baseURL (from this doc: https://www.mongodb.com/docs/atlas/app-services/tutorial/swiftui/), and then I refactored that to simply inline the value. It seems to work fine without that parameter tho.

Jez. Hope this is useful for someone else.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.