Reputation: 362
If I want to store the token retrieved from the server in a place where i can access to it from wherever in my code to keep sending it as a json header to my server, where i should store it? is there any other recommendation you can give me to use with token auth?
Upvotes: 2
Views: 9719
Reputation: 1186
It seems safe to store it in the app's private SharedPreferences and make a static helper to get it. Only your app will have access to this.
from Android API documentation
File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
package net.rouk1.helper;
import android.content.Context;
import android.content.SharedPreferences;
public class TokenSaver {
private final static String SHARED_PREF_NAME = "net.rouk1.SHARED_PREF_NAME";
private final static String TOKEN_KEY = "net.rouk1.TOKEN_KEY";
public static String getToken(Context c) {
SharedPreferences prefs = c.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return prefs.getString(TOKEN_KEY, "");
}
public static void setToken(Context c, String token) {
SharedPreferences prefs = c.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(TOKEN_KEY, token);
editor.apply();
}
}
Upvotes: 7