Search code examples
androidasp.netiphonec#-4.0wcf-rest

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


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 requirement is this.1) If user has cell phone say abc phone with IMEI "abc1234". He logs in from this phone. Now if he tries to log in from his 2nd phone than he should not be allowed to login from 2nd phone. He should first log out from abc device.

now there is few complications 1) if user is logged from abc. With out loging out he formats his phone or uninstalls the app. Then how should I handle login from same device or other device.

Basically I want to know about strategies or good practises for such type of scenarios.


Solution

  • 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");