![]() This is also basic definition of Singleton design pattern. Since it is static if there is no instance available it just creates instance only once and when you call it always returns this static instance. ![]() Step 1: Please refer singleton pattern, we will create one class that's responsible for returning our retrofit instance. (Maybe retry refresh token couple of times before logging out the user) The flow is: If refresh token fails, logout current user and require to re-login. Creating Project Here I have created an Android Studio project with package also Activity as MainActivity and layout as activitymain. You can use ampoline() or blockingGet() when refreshing your token to block that thread.Īlso inside authenticate method you can check if token is already refreshed by comparing request token with stored token to prevent unnecessary refresh.Īnd please do not consider using TokenInterceptor because it is edge case and not for everyone, just focus on TokenAuthenticator.įirst of all refreshing token is a critical process for most apps. In this second part of the tutorial, we will create an Android app with Retrofit to upload an image to the Node.js server we created before. Please make synchronous requests when refreshing your token inside Authenticator because you must block that thread until your request finishes, otherwise your requests will be executed twice with old and new tokens. To beat this issue just add synchronized keyword to your TokenAuthenticators authenticate method. For example when your activity and your service make requests concurrently. ![]() If you are making requests from several places your token will refresh multiple times inside TokenAuthenticator class. Actually I am using Dagger + RxJava + Retrofit but I just wanted to provide an answer to demonstrate logic for future visitors. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2023
Categories |