1

I'm currently stucked planning my architecture (MVVM) and flow for the next app.

I want the User to tap 'log in' button to get token from api. Then token is used to authorize some of api calls. I'd like to store token in AuthService (singleton) class, but to do that I have to inject my ApiService (singleton) to AuthService to make first login request. Then I need the token back in ApiService injected from AuthService to be able to make authorized requests. The problem is that both ApiService and AuthService depend on each other (I'd like to avoid creating another Volley instance in AuthService just to make one-time get token request - prefer making requests using ApiService only).

The qestion is... how should I go with it? What is the pro-way of solving problem like this one? Any hints much appreciated!

1 Answer 1

1

You can have a shared preference provider in your dependency injection tree, and have a method in it that stores/retrieve the user token. Then you can inject the shared preference in your repository, and in your repository, you will have the ApiService which you can pass the token to from the injected shared preference instance.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alaa. SharedPreferences are not the recommended way to store tokens and user data due to root access, that's why I prefer to keep it 'in memory' (in AuthService). But you've just opened my mind that there shuld be another TokenProvider that will get token using ApiService, then provide it to AuthService. That way ApiService will have AuthService instance and will be able to get token from there. I knew its was easy but somehow missed it... LOL :D. Thanks and have a good day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.