I work with SQLite, and I have a registration form. When the user is registered, the data is stored in a table, but the application asks to log in every time. Is it possible to check whether the current user has already been registered, and if so, then skip the registration?
3 answers
I think you need to use SharedPreferences ... When a user goes through the registration form or logIn and everything is successful, then we save it logIn and password in SharedPreferences and the next time he opens the application, check on onCreate, retrieve the recorded data and check if they are in the table, if so then we return a specific user. When the user clicks logOut, then erase the data from SharedPreferences
- Yes, like this. If the user is meant alone, then it suffices to find out if there is even at least one entry in the user table. - Yuriy SPb ♦
- But in this case the user table is not needed if it is implied that the user is one)) - Aleksey Timoshchenko
- It is easier to make the variable Boolean isLogin; and save it exactly at launch and exit, using of course SharedPreferences. It remains before the withdrawal of the registration form to make sure that if (isLogin == true) ... - LEO
- Yes, but in that case, how will you know which particular user to return? - Aleksey Timoshchenko
SharedPreferences settings = getSharedPreferences(PREFRENCES_NAME,Context.MODE_PRIVATE); String email = settings.getString("email", ""); String password = settings.getString("pword", ""); inputEmail.setText(email); inputPassword.setText(password); if(inputEmail.getText().toString().length() != 0 && inputPassword.getText().toString().length()!=0){ //code допуск }//проверка на длину
The login data is inserted (in this case, email and password) into the cache, then we check if the lengths of lines that are not zero in the cache, then give the go-ahead for access to the content, back and so clearly. Ps create a separate PREFRENCES_NAME for this case so that the extra "other" data is not stored in it.
- hmmm ... I just implemented for you the code that was given in the description above "I think I need to use SharedPreferences ... When the user goes through the registration form or logIn and all is well, then save it logIn and password in SharedPreferences and next time when he opens the application, then in onCreate, do a check, retrieve the recorded data and check if they are in the table, if so, return a specific user. " - iFr0z
Sure you may. If you write down any data about the user during his registration, then just check the availability of this data in the database before showing the registration window ....
- Yes, but with what to compare? Suppose the user logged in the table was recorded ID, name and password ... Then he closed the application and opened again ... How the application will understand the same user or not? What should the application check? - Aleksey Timoshchenko
- @AlekseyTimoshchenko, if the user is not alone on the device, then there is no way to determine it and you will have to always force it to login. It is absolutely impossible to determine whether this person is the same or not. We’ll not try to do this by locating the face with a front-facing camera, finger scanner and DNA analysis. - Yuriy SPb ♦
- I agree, but when I once went on my device, let's say in the VK application, I don’t enter my data every time ... Apparently they know something about my DNA))) - Aleksey Timoshchenko
- one@AlekseyTimoshchenko, the trouble is that if another person takes your phone, then the login from VK will not be lost. - Yuriy SPb ♦