How to save password in sharedpreference? - android

I have done login and registration activity using rest API. But in login I used only Mobile number editext. And in registration I used password. So I want to use that password in login. Means user should enter that password while he used during registraton.
So how to do that?

If you're going to store passwords, make sure you store them in EncryptedSharedPreferences. You do not have to encrypt them manually as the library will take care of it.
If you need/want more explanation on this and how it works, check out some articles about it on the web - there are plenty.

If your using Kotlin, saving password in Shared Preferences could look like this:
Initialize Shared Preferences:
var prefs: SharedPreferences
prefs = getSharedPreferences("name_of_your_file", Context.MODE_PRIVATE)
Save password to it:
with (prefs.edit()) {
putString("password", etPasword.text)
apply()
}
If you want to access this stored password, you can do it like this:
val password = prefs.getString("password", "default_value")

Related

Hardcoding user name and password in android app

I am creating an App and i want to store username and password in app itself. The username password is not entered by user. The credentials is common for everyone. I want to save the username and password in app code itself. What is the secure method to save in android?
You can use SharedPreference to store data permanently in the Android app.
In your Activity,
//get the sharedPrefs
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
//store the data in sharedPrefs file
with (sharedPref.edit()) {
putString("Some_key_user_name",userNameText)
commit()
}
//read the stored data form sharedPrefs
val storedUserName = sharedPref.getString("Some_key_user_name", defaultValue)
For the password, you can use the same approach. However, if the password is so precious(which it is in most cases) you can encrypt the data stored in sharedPreference file. Look ahead with this
As an example in my Activity,
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
with (sharedPref.edit()) {
putString("MY_HELLO_WORLD_KEY","helloWorld")
commit()
}
You can use key store to save your private keys securely
check this
Securely Storing Keys in Android Keystore
you can also use EncryptedPreferences to store simple data in an encrypted way.
check How to save secret key securely in android

Retrieving SharedPreferences

I'm working with an android application, and when my application is open I want to log in. I'm using shared preferences for saving data.
How can I control three cases: if his credentials are null, if that user exists and does not have to register and save his credentials again and also if username and password he entered is correct and is the same with them he used when entered for the first time.
Use this code to retrieve values from sharedPreferences.
SharedPreferences sharedPref ;
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int status=sharedPref.getInt("status",0);
if(status==1){
//do something here you want to do
}else if(status==2){
//do something here you want to do
}
Read the documentation here http://developer.android.com/reference/android/content/SharedPreferences.html
Also check this http://www.tutorialspoint.com/android/android_shared_preferences.htm

How to have android app remember user data/profile?

Making a app that asks user to create a profile, wondering where I should get started in having the app remember this data user inputs? Any tutorials or suggestions would be appreciated.
Thanks,
Grant
I suggest you use SharedPreferences, unless you have a lot of information to store.
After the user successfully logged in, store his information in your Preferences.
For example, to store the username :
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("User", MODE_PRIVATE);
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString("username", your_user_name);
editor.commit();
Each time the user accesses the login activity, you can check if the username is already stored in the preferences :
if (mPreferences.contains("username")) {
// start Main activity
} else {
// ask him to enter his credentials
}
When the user logs out, don't forget to delete the username key from your preferences :
SharedPreferences.Editor editor = mPreferences.edit();
editor.clear(); // This will delete all your preferences, check how to delete just one
editor.commit();
Insert the data to a SQLite database or use a plain file. The former is recommended for big apps.
Checked out SharedPreferences. Some examples here: SharedPreferences Tutorial
If you only have one user at once and just need to store simple user data like user name, email, id, you can store string/int/... format data in it. Or if you have server for storing user data, you can store credentials in SharedPreference and use the credentials to get data from your server.

Android Login with server integeration

In my application I am using a login process. In that I have username and password field. Once I login, the username and password will be there even when you exit the app or kill the app.
How can I set like that?
In android There is functionality of sharedpreferences You can save your username and password in shared preferences and check that if sharedpreferences are not null then display username and password in textField
You can use SharedPreference as below:
SharedPreferences m_sharedPreference=getSharedPreferences("MyPrefs",MODE_PRIVATE);
SharedPreferences.Editor m_editor =m_sharedPreference.edit();
m_editor.putString("UserName", m_etUsername.getText().toString());
m_editor.putString("Password", m_loetPassword.getText().toString());
m_editor.commit();

Shared Preference problem in android app

in my app the first activity is a sign in page. In the edit boxes i am typing the user name and password. Those values are been move to an api and in return i am getting the userid from the server as an xml file.
I am parsing the xml file and storing the value in shared preferrence as follows
SharedPreferences.Editor IdEditor = Id.edit();
IdEditor.putString("useridValue", chap.getid());
IdEditor.commit();
And in the next time when the user opens the app i want to check whether it is already signed i or not. How to check this using the value stored in Shared preference
is your Id class extending SharedPreferences ?
maybe
String userId = Id.getString("useridValue");
If your preference is stored in the default preference then you can
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String userId = prefs.getString("useridValue");
on a side note you shouldn't really use a capital I on the IdEditor variable it should probably be idEditor
Check whether this entry already exist in shared preference, using:
id.containskey("useridvalue")

Categories

Resources