0

My first activity is the login activity with simplest form and hardcoded credentials for this purpose. POST request is sent to the API and the token is returned as a result. Everything works fine. Now the way I implemented it is I have an intent placed inside the onSuccess method in the onClick event inside the login method. Here's the code:

Login login = new Login("{hardcodedEmail}", "{hardcodedPassword}");
Call<User> call = userClient.login(login);
call.enqueue(new Callback<User>() {
    @Override
    public void onResponse(Call<User> call, Response<User> response) {
        Toast.makeText(MainActivity.this, response.body().getToken(), Toast.LENGTH_LONG).show();
        String token = response.body().getToken();
        Intent i = new Intent(MainActivity.this, TestActivity.class);
        startActivity(i);
   }

   @Override
   public void onFailure(Call<User> call, Throwable t) {
       Toast.makeText(MainActivity.this, "Error: \n" + t.getMessage(), Toast.LENGTH_LONG).show();
   }
});

This code has a simple toast to show me the token and say that everything's ok. It does that, I don't get any errors on this page. But on the next page, I'm trying to retrieve some data from the API and display it, but I'm getting error 401, or unauthorized access. Meaning my credentials didn't stay.

Is there something else I need to do other than just skip right into the next activity to keep the credentials alive?

Derrick
  • 55
  • 1
  • 1
  • 8

1 Answers1

1

You can pass token string through Intent from your MainActivity to TestActivity. Feel free to try this:

call.enqueue(new Callback<User>() {
    @Override
    public void onResponse(Call<User> call, Response<User> response) {
        .....

        String token = response.body().getToken();
        Intent i = new Intent(MainActivity.this, TestActivity.class);

        // pass token string through Intent
        i.putExtra("TOKEN_STRING", token);

        startActivity(i);
   }

Then in TestActivity, get token from Intent pass in:

// declare token as member variable
private String token;

@Override
public void onCreate(Bundle savedInstanceState) {
    .....

    Intent intent = getIntent();
    this.token = intent.getStringExtra("TOKEN_STRING");

    // you can do whatever you want with token later
    // eg: use token for authentication when issue next network call

    ....
}
Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42
  • Thanks for the answer. When I obtain the token, where do I pass it in order to be authenticated? Sorry I'm still learning Retrofit. – Derrick Feb 01 '18 at 00:13
  • You have to check the API documentation that you are using. My guess is, first you issue login calls to get token, this is what you did in `userClient.login(login);`. Then whenever you issue `authenticated API calls`, token might need to be pass via some way, for example: set it as HTTP header, etc. Based on this, server is able to differentiate between genuine `logged-in` user vs anonymous users. So take a look on your API documentation. – Shuwn Yuan Tee Feb 01 '18 at 00:32
  • Thanks, I'll check it out and report back. – Derrick Feb 01 '18 at 01:36
  • Btw if you find my answer helpful, would you mind to upvote it or accept as answer? Thanks. – Shuwn Yuan Tee Feb 02 '18 at 03:04
  • I don't have enough rep to upvote you, but I marked your answer as a solution. Thanks again. Btw all that was needed was to pass the token as an authorization header to the client. – Derrick Feb 03 '18 at 10:53