0

I'm implementing the login fragment of a new app. And I'm dealing with the issue of saving the access token somewhere to use it for every call in the app.

I was wondering where I should save it. Should I use shared preferences as usual or I should use in a way I don't know the viewModel in order to store that token there?

The problem is that if I store the accessToken in the LoginViewModel, I would need to call that viewModel in every views where I'll need the accessToken. It doesn't seem the best option.

Any idea? Thank you

Filnik
  • 352
  • 1
  • 12
  • 33
  • 1
    https://stackoverflow.com/questions/15073322/where-should-i-store-authentication-token-in-android?rq=1 ; https://stackoverflow.com/questions/30485858/use-sharedpreferences-to-store-authentication-token?rq=1 ; https://stackoverflow.com/questions/57402457/how-to-save-a-token-secretly-in-android?rq=1 – OneCricketeer Jan 21 '20 at 14:51

1 Answers1

0

There is typically a one-to-one relationship between a view and its view model, which means ViewModel is used to provide logic for a View or a page. If your requirement is to use that token across your page in your app, then ViewModel is not an option, because it will not make sense if you have to provide LoginViewModel in another view other than Login Page.

Save the token into shared preference is the better approach for me, and it is simple very simple to implement. You can use AndroidViewModel if you want to save a shared preference value from a ViewModel, because AndroidViewModel will provide Context which can be used to create shared preference.

Devara
  • 75
  • 2