I have built an Rest API using rails and doorkeeper. I'm using assertion grant flow and facebook login to create and login user in android client.
I've successfully logged in and got access token from my server using retrofit. The access token has token, refresh_token, token_type, expires_in and created_at info.
I have following option to manage and maintain this token while user is browsing my android app.
- Save all the info from access token in SharedPreferences when user opens the app and logs in. And now it leads to main activity on successful login, I access shared preferences and access api using the token.
- Second option is, I pass the access token object as parcelable object and get access_token in next activity using this object.
I can check if the access_token is expired by comparing it with current time and created_at time. everytime before accessing the api. If it does expire, I access a new token using refresh_token.
I think both of the above approaches leads to code duplication and repeating the same thing.
- If there is any other approach to do it, that'll be great as well. I think it'd be great if my retrofit client can manage whether token is expired or not and if it does expire, it should request a new token. In short I'm thinking of using some sort of Interceptor. But I don't how to do it.
Which of the above approach is more suitable.
Thanks!