A
A
Artur Abdeev2020-12-02 14:35:56
Java
Artur Abdeev, 2020-12-02 14:35:56

How to work with refresh token in android development?

My application can perform authentication, authorization and obtain profile information. Everything is done through a token. I need to add a situation where the token has expired. (Working with RefreshToken). I don't know how to do it right. I don't know where is the best place to check this.

I am posting part of the code. It is unlikely that it will help. But let it lie.

This is MainActivity when the application starts, it checks for Null token

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        SharedPreferences sharedPref = getApplicationContext()
                .getSharedPreferences("userInfo", AppCompatActivity.MODE_PRIVATE);
        String token = sharedPref.getString("access_token",null);

        if (token != null) {
        Intent intent = new Intent(MainActivity.this, Profiile.class);
            startActivity(intent);
            finish();
        }
    }


This is Singleton for Retrofit



public class NetworkService {
    private static NetworkService mInstance;
    private static final String BASE_URL = "https://minesrv.ey.r.appspot.com";
    private Retrofit mRetrofit;

    private NetworkService () {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        SharedPreferences preferences = App.Companion.getInstance().getSharedPreferences("userInfo", AppCompatActivity.MODE_PRIVATE);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .addInterceptor(new AccessTokenInterceptor(preferences))
                .build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


I'm not waiting for any solutions. Can you just say how productive it is to do it. How would Senior do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2020-12-02
@YuryBorodkin

As a senior - a long time to explain, security, abstract-extensibility, blah blah, you don't need it yet.
Retrofit has an interceptor mechanism , you can make your own, where you will request a new token for a token expiration error and repeat the request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question