3

I'm trying to use parse locally (with MongoDB) with my iOS mobile app. I've installed node, mongo, parse, parse dashboard. I've created a pod file for my iOS project (XCode), installed parse with it. I put this code into the application didFinishLaunchingWithOptions method of the AppDelegate:

import Parse

Parse.enableLocalDatastore()
        
        let parseConfig = ParseClientConfiguration {
            $0.applicationId = "XXXXXXXXXX"
            $0.clientKey = "YYYYYY"
            $0.server = "http://(IP addr):(port)/parse"
        }
        Parse.initialize(with: parseConfig)

I'm starting mongoDB from the terminal and

the parse server also from the terminal:

--appId “XXXXXXXXXX” --clientKey "YYYYYY" --masterKey “ZZZZ” --databaseURI mongodb://localhost/parse

In my XCode app's viewController's viewDidLoad method I put this code:

      import Parse
      
      let gameScore = PFObject(className:"GameScore")
        gameScore["score"] = 1337
        gameScore["playerName"] = "Sean Plott"
        gameScore["cheatMode"] = false
        gameScore.saveInBackground {
            (success: Bool, error: Error?) in
            if (success) {

            } else {
                print("saveInBackground error: ", error!)
            }
        }

When I run my XCode swift app I get this error:

[Error]: unauthorized (Code: 0, Version: 1.17.2)
error:  Error Domain=Parse Code=0 "unauthorized" UserInfo={error=unauthorized, NSLocalizedDescription=unauthorized, temporary=0}

This error comes from the saveInBackground Parse method. I also tried retrieving data from the parse server instead of saving, I got the same error:

let query = PFQuery(className:"GameScore")
        query.whereKey("playerName", equalTo: "Michael Yabuti")
        do {
            let scoreArray = try query.findObjects()
            print(scoreArray)
        }
        catch {
            print("findObjects error: ", error)
        }

From my parse dashboard I can see that the server is running, but no data is saved there from my mobile app

I've never done anything like this, please someone give me some pointers what the problem could be. Any help would be very much appreciated

MongoDB gives me this error:

AssertionException handling request, closing client connection: xxx SSL handshake received but server is started without SSL support
2019-02-13... I NETWORK  [initandlisten] connection accepted from x.x.x.x:yyyy
Tio
  • 73
  • 1
  • 2
  • 14
  • Did you verify your client key with parse server ? – CodeChanger Feb 13 '19 at 07:22
  • 1
    Did you get any error while execute `npm start` locally? – CodeChanger Feb 13 '19 at 07:27
  • @CodeChanger, thank you for the help :) I'm not using npm start to start the parse server. You can find the command I use to start parse server in my question: "--appId “XXXXXXXXXX” --clientKey "YYYYYY" --masterKey “ZZZZ” --databaseURI mongodb://localhost/parse" THIS IS HOW I START the parse-server: I cd into the parse-server folder of the node-modules and run this command from there. After that I check it with a parse server dashboard and it works, the parse server is running, it's accessible. Yes, I start the parse server with the clientID. – Tio Feb 15 '19 at 00:26
  • Sorry, I had to use some capital letters above to make my message easier to read...as StackOverflow doesn't let us put enter into our message :( – Tio Feb 15 '19 at 00:29
  • When I start the parse-server and the parse dashboard I don't get any error, everything seems right. Even after starting MongoDB (first I start this) I don't get any errors, only when I run my XCode project – Tio Feb 15 '19 at 01:22

1 Answers1

1

From the error message I would assume that you have to enable SSL on mongod. This can be done on startup with the following argument: --sslMode requireSSL. When you do that, you either have to provide a pem file with --sslPEMKeyFile or a certificate selector with --sslCertificateSelector

A pem file is a bit easier to setup. To do that, do the following:

// Create a crt and key file
openssl req -newkey rsa:2048 -new -x509 -days 365 -nodes -out mongodb-cert.crt -keyout mongodb-cert.key
// concat both files to get a pem file
cat mongodb-cert.key mongodb-cert.crt > mongodb.pem

Now you can start mongod with SSL support:

mongod --sslMode requireSSL --sslPEMKeyFile mongodb.pem <plus any other options>

How to configure mongodb with SSL is explained in an official tutorial: https://docs.mongodb.com/manual/tutorial/configure-ssl/

mbuechmann
  • 5,413
  • 5
  • 27
  • 40
  • thank you @mbuechmann I tried it, everything went well without errors when I ran the terminal commands - you wrote - on my Mac. However, when I started my XCode project I got a similar error as earlier: nw_socket_handle_socket_event [C1.1:2] Socket SO_ERROR [61: Connection refused] [Error]: unauthorized (Code: 0, Version: 1.17.2) Error Domain=Parse Code=0 "unauthorized" UserInfo={error=unauthorized, NSLocalizedDescription=unauthorized, temporary=0} – Tio Feb 17 '19 at 23:54
  • My swift code in XCode is correct, I copied the whole code from the Parse documentation website: https://docs.parseplatform.org/ios/guide/ – Tio Feb 17 '19 at 23:55
  • It is possible, that now one issue is solved and you are getting the next one. Are there any other error messages in mongodb? It now looks like that you have to send credentials from your iOS app when connecting to the parse server. – mbuechmann Feb 18 '19 at 07:38
  • there are no errors in mongoDB. Everything looks fine. I'm sending my credentials from the iOS app. That's the first code I added in my discription above (the one that starts with import Parse and Parse.enableLocalDatastore() – Tio Feb 19 '19 at 22:52
  • Then those credentials are incorrect. The error message states that you are not authorized (`unauthorized`). But this is another problem that warrants another question. – mbuechmann Feb 20 '19 at 07:47