Search code examples
iosrealmmongodb-atlas

MongoDB Realm Atlas App Services Auth fail on some devices


I have an app that uses the MongoDB Atlas App Services Authentication service, using Sign In with Apple as my auth provider. It works well, but on one device and all simulators it fails with the following error.

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: ‘<’}

When breaking trough the login code, it is apparent that the failure occurs here; enter image description here

At this point, the response.body property is a HTML document, which will naturally fail to parse as 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"...

My login code is pretty much exactly what's stated in the documentation,

    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 {
            struct NoCredentialsError: Error { }
            presentError(title: "Login Error", error: NoCredentialsError())
            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)
            }
        }
    }

The tokenString variable has been verified to contain an actual-looking token.

I've re-installed Realm, updated to latest version, purged swiftpm caches etc. This issue has persisted on simulators since March. On most physical devices and Mac (Designed for iPad) it works as intended.

Is there some odd configuration trick I'm missing? Is this a bug purely on MongoDB's end?


Solution

  • 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.

    Jez. Hope this is useful for someone else.