1

I'm currently building an app that has Parse at its backend. I've realized that a user can log in to his account in multiple devices. Is there a way I could restrict this to only one device at one time? If so can you kindly explain the method. Thanks in advance.

Chehanr
  • 81
  • 9

2 Answers2

0

you need to monitor the application will your web environment (webservice for example) As soon as someone you login you must disconnect the other devices connected to the same user.

You can analyze it by IMEI who made the last login request and send a command to the other devices of the same user to remove access.

0

I came here looking for a solution and didn't find one. Through trial and error, I have solved it. I am a novice developer and do not know if this is the best practice, but it works. When the user attempts to log in execute the following.

    public void login(View v) {
    //  Create string variables for data.
    String username = et_username.getText().toString().toLowerCase().trim();
    String password = et_password.getText().toString();

    //  If a user is already on this device, log them out.
    //  this will happen when the force log out happens, the device will 
    //  simply catch the invalid session, but still have that user data, just unavailable
    user = ParseUser.getCurrentUser();
    if (user != null)
        user.logOutInBackground();

    ParseUser.logInInBackground(username, password, new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException e) {
            final ParseQuery<ParseObject> query = new ParseQuery<>("_Session");
            query.include("user");
            query.whereEqualTo("user", ParseUser.getCurrentUser()); //  Only get sessions for the specific user.
            query.addAscendingOrder("createdAt");  //  Place them in chronological order, with the oldest at the top.

                query.countInBackground(new CountCallback() {
                    @Override
                    public void done(int count, ParseException e) {
                        if (count > 1) {
                            try {
                                query.getFirst().deleteInBackground();
                            } catch (ParseException e1) {
                                Toast.makeText(LoginActivity.this, e1.toString(), Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                });

            if (user != null) {
                emailVerified = user.getBoolean("emailVerified");

                if (emailVerified) {
                    //  Update the database to track their log in.
                    user.put("loggedIn", true);
                    user.saveInBackground();
                    if (!deliquent) {
                        //  Launch the MainActivity.
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    else {
                        Intent intent = new Intent(LoginActivity.this, CardActivity.class);
                        startActivity(intent);
                    }
                }
                else {
                    Toast.makeText(LoginActivity.this, getResources().getString(R.string.verify), Toast.LENGTH_SHORT).show();
                }
            }
            else {
                Toast.makeText(LoginActivity.this, "From LoginActivity line:213\n" + e.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });

}
MarvyP
  • 21
  • 7