Reputation: 27
Hello i am using dynamodb and i want to know how do i maintain a session when the user logs in.
Following is my login code
Runnable runnable = new Runnable() {
public void run() {
Users user = mapper.load(Users.class,username);
System.out.println(user.getPassword());
System.out.println(username);
if (user != null && user.getPassword().equals(password)){
//System.out.println("Correct");
runOnUiThread(new Runnable() {
@Override
public void run() {
setContentView(R.layout.activity_account);
}
});
}
and this is my user class where i set and get all entities
public class Users {
private String id;
private String email;
private String username;
private String password;
private String role;
@DynamoDBHashKey(attributeName = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@DynamoDBAttribute(attributeName = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
same goes for all other variables as well.
Please look at these codes and tell me how i could store sessions as i want the id of the logged in user.
Upvotes: 0
Views: 245
Reputation: 163
V1
you can use SharedPreferences like below
public class Prefs {
public static void putPref(String key, String value, Context context){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getPref(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
}
after you have to store
Prefs.putPref("the key","the value",your context);
after to get your stored data
String userId = Prefs.getPref("the key",your context);
you can use that where you want in your App .
V2
use Gson add this to your build.gradle
compile 'com.google.code.gson:gson:1.7.2'
and convert your user object to json like this
Gson gson = new Gson();
User user = ...;
String json = gson.toJson(user);
after store your user
Prefs.putPref("user",json,your context);
to get your user
String json = = Prefs.getPref("user",your context);
User user = null;
if(json != null)
user = gson.fromJson(json,User.class);
thats all
Upvotes: 2