I’m implementing rest API using volley library for my android assignment and I want to know how to save the token because every other API needs the access and refresh token, Should I save it in a database? How do I access the stored token when calling other APIs?
This is my webApi class,
public void login(String email, String password, final APIListener listener) {
String url = BASE_URL + "/oauth/token";
JSONObject jsonObject = new JSONObject();
try {
JSONObject userJSON = new JSONObject();
userJSON.put("email",email);
userJSON.put("password", password);
jsonObject.put("user",userJSON);
jsonObject.put("grant_type", "password");
Log.d("Json Object", jsonObject.toString());
Response.Listener<JSONObject> successListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
Log.d("th response",response.toString());
Gson obj = new Gson();
Authentication authObj = obj.fromJson(response.toString(), Authentication.class);
Log.d("successObj",authObj.getSuccess());
listener.onLogin(authObj);
}
catch(Exception ex){
Log.e("Volley onResponse Error",ex.toString());
Toast.makeText(mApplication, "JSON exception", Toast.LENGTH_LONG).show();
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Json Error Response",error.toString());
Toast.makeText(mApplication, "Invalid Login", Toast.LENGTH_LONG).show();
}
};
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, successListener, errorListener);
mRequestQueue.add(request);
}
catch (JSONException exception){
Log.e("Login exception",exception.getStackTrace().toString());
Toast.makeText(mApplication, "JSON Exception", Toast.LENGTH_LONG).show();
}
}
This is the activity class,
//login
EditText emailField = findViewById(R.id.email);
EditText passwordField = findViewById(R.id.password);
Button loginBtn = findViewById(R.id.LoginBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailField.getText().toString();
String password = passwordField.getText().toString();
final Model model = Model.getInstance(LoginActivity.this.getApplication());
model.login(email, password, new AbstractAPIListener() {
@Override
public void onLogin(Authentication authentication){
if(authentication.getSuccess().equals("true")) {
model.setAuth(authentication);
Toast.makeText(LoginActivity.this, "Login success!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
}
else{
Toast.makeText(LoginActivity.this, "Invalid Login!", Toast.LENGTH_LONG).show();
}
}
});
}
});
2
Answers
You can store your access token in Shared preferences
Auth0 provides a utility class to store tokens. Its better to use that utility library. There are two classes you can use to manage credentials:
a combination of RSA and AES algorithms along with Android KeyStore.
Link to documentation: Auth0.Android Save and Renew Tokens