0

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?

Simple
  • 827
  • 1
  • 9
  • 21
  • Do you need to check if the user that is trying to login in some device is already logged in another device? Or do you want to check if the user is already logged in the current device so they don't need to log in again? – Davi Macêdo Jul 26 '19 at 16:11
  • @DaviMacêdo I wanna check if the user is already logged in the application, and if other person with their password logs in so it should disconnect the previous user, I just don't want 2 persons or more using same login and sharing that with others. – Simple Jul 26 '19 at 19:26

1 Answers1

1

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
Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11
  • Thank you for the idea, but the problem is that there is no Parse API for C++, also in my winforms application since it is C++ CLI, therefore lambda expressions are not even supported. I gotta do it all by `cURL` commands. – Simple Jul 28 '19 at 19:02
  • 1
    I think you didn't understand the solution I proposed. The cloud code function lives in the server side (and not in your C++ application). You just need to place the code I sent you in a `main.js` file and start your parse server with it. In the client side (your C++ application), you just need to run the curl command that I sent you. It will invoke the code in the server side. – Davi Macêdo Jul 29 '19 at 01:33