Search code examples
c++.netcurlparse-platformparse-server

How to prevent multiple logins in Parse Server using cURL


Since my application is C++, there is no Parse API available, so I have to do all the stuff using cURL. My application authenticates user, and should not let multiple logins, by looking at this documentation I don't see a workaround to check if the user is already logged without compromising it with too many requests, a solution that doesn't require constant database access would be the best, like this answer which gives a good idea how to do that, but is it possible to get such result using cURL only?


Solution

  • For your case, I recommend you to write a cloud code function that will be responsible for receiving the username and password, log in parse server, delete the other existing sessions for the same user (so other devices will be logged out), and return the session token. Then you can call this cloud code function from your C++ application using a single curl command. The codes should be something like below:

    Cloud code function:

    Parse.Cloud.define('logIn', async req => {
      const username = req.params.username;
      const password = req.params.password;
      const user = await Parse.User.logIn(username, password);
      const sessionToken = user.getSessionToken();
      const oldSessionsQuery = new Parse.Query(Parse.Session);
      oldSessionsQuery.equalTo('user', user);
      oldSessionsQuery.notEqualTo('sessionToken', sessionToken);
      const oldSessions = await oldSessionsQuery.find({ useMasterKey: true });
      await Parse.Object.destroyAll(oldSessions, { useMasterKey: true });
      return sessionToken;
    });
    

    Curl command:

    curl -X POST \
      -H "X-Parse-Application-Id: YOUR_APP_ID" \
      -H "X-Parse-REST-API-Key: YOUR_REST_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{ "username": "the user name goes here", "password": "the password goes here" }' \
      https://your.server.url/functions/logIn