How to make user login from only one device at a time

后端 未结 7 2876
予麋鹿
予麋鹿 2021-02-04 16:47

I have a RestAPI which when hit authenticate the user. This api is exposed to android and ios developers they hit this api to allow user to login to our app

My requireme

相关标签:
7条回答
  • 2021-02-04 17:13

    Try, to save a boolean variable. Which will be mark as 0 and the user will be allowed to login. On login, the value will be change to 1 and on logout change it back to 0. User will only be allowed to login when the value of boolean character is 0.

    0 讨论(0)
  • 2021-02-04 17:17

    Good practice would be let the user login from second device and logged out him from first device it would be better from user experience as well as will be easy to handle e.g

    I logged in from DeviceId A (update DeviceId in db against the user) then I try to logged in from DeviceId B (again update DeviceId overriding previous DeviceId in db against the user)

    Now If I make a request from DeviceId A, match the DeviceID in DB, it will return false. Send user back to login page.

    0 讨论(0)
  • 2021-02-04 17:17

    You can use SharedPreference for storing user login detail after logging and second time you run the app, check that login detail is available or not. And after logout you have to delete shared preference.

    For more detail please visit below links:

    http://www.tutorialspoint.com/android/android_shared_preferences.htm http://developer.android.com/reference/android/content/SharedPreferences.html

    0 讨论(0)
  • 2021-02-04 17:18



    I suggest that you maintain a session for a login. Whenever there is a successful login, you would provide a unique session identifier to the user. This session id can subsequently be used for further requests/calls from the user.

    For scenario of user logging in from another number, an active session will mark this second login request as invalid.

    Keep an aging mechanism for each session. This way an inactive session, caused by phone getting disconnected or off, will get terminated.

    0 讨论(0)
  • 2021-02-04 17:28

    Use SharedPreferences for solution,

    For eg.

     public class Pref_Storage {
        private static SharedPreferences sharedPreferences = null;
    
        public static void openPref(Context context) {
            sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.app_name),
                    Context.MODE_PRIVATE);
        }
    
        public static void deleteKey(Context context, String key) {
            HashMap<String, String> result = new HashMap<String, String>();
    
            Pref_Storage.openPref(context);
            for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                    .entrySet()) {
                result.put(entry.getKey(), (String) entry.getValue());
            }
    
            boolean b = result.containsKey(key);
            if (b) {
                Pref_Storage.openPref(context);
                Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
                prefsPrivateEditor.remove(key);
    
                prefsPrivateEditor.commit();
                prefsPrivateEditor = null;
                Pref_Storage.sharedPreferences = null;
            }
        }
    
        public static void setDetail(Context context, String key, String value) {
            Pref_Storage.openPref(context);
            Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
            prefsPrivateEditor.putString(key, value);
    
            prefsPrivateEditor.commit();
            prefsPrivateEditor = null;
            Pref_Storage.sharedPreferences = null;
        }
    
        public static Boolean checkDetail(Context context, String key) {
            HashMap<String, String> result = new HashMap<String, String>();
    
            Pref_Storage.openPref(context);
            for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                    .entrySet()) {
                result.put(entry.getKey(), (String) entry.getValue());
            }
    
            boolean b = result.containsKey(key);
            return b;
        }
    
        public static String getDetail(Context context, String key) {
            HashMap<String, String> result = new HashMap<String, String>();
    
            Pref_Storage.openPref(context);
            for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                    .entrySet()) {
                result.put(entry.getKey(), (String) entry.getValue());
            }
    
            String b = result.get(key);
            return b;
    
        }
    }
    

    Use:

    Before login check login_flag:

    if (Pref_Storage.checkDetail(getApplicationContext(), "login_flag"))
    {
        // Home Screen
    }
    else
    {
        //Display Login Screen
    }
    

    After Login set login_flag:

    Pref_Storage.setDetail(getApplicationContext(), "login_flag", "0");
    
    0 讨论(0)
  • 2021-02-04 17:33

    Hello you can do one thing. Track user login status. like

    1. while user login add entry that user is active or login
    2. now while user is trying to login with another device at that time check that user is login/active or not. if user is active then don't allow them for login and display message that logout from your old device. You can also put auto logout from last device functionality also.
    3. While user is logout at that time change user status active/login to not-active/logout so by this way you can manage this.

    You have to pass this user status with your login web Service or API in android and in website you can directly check from DB.

    0 讨论(0)
提交回复
热议问题