Peter-Schorn/SpotifyAPI

Persistent storage for auth info

Closed this issue · 6 comments

First of all, thanks for this repository! It looks quite nice and useful for my use case.

I've not found an easy way to persistently store and load the auth info - especially the refresh token. Right now, I have to login every time the app starts.

I guess, you have a solution for that. Can you share it and/or add it to the documentation, please?

@dominikdassow Thanks for your comment, Dominik. I'm glad you find the library useful! In fact, I do have a solution for persistent storage of the authorization info. The authorizationManager generic property of SpotifyAPI is what stores the authorization information and it conforms to Codable. This is because the the SpotifyAuthorizationManager protocol—which is what the type of authorizationManager must conform too—inherits from Codable. Therefore, the solution is to encode this property into data using JSONEncoder and store this data in persistent storage. Note that authorizationManager is a mutable var property, so you can retrieve an instance of a type that conforms to SpotifyAuthorizationManager from persistent storage and assign it to this property as many times as you need to.

However, you must keep in mind the fact that the access token expires after an hour. This library automatically refreshes the access token when necessary, so you shouldn't have to worry about refreshing it manually, although you do have to make sure that you re-save the authorizationManager property to persistent storage every time this happens. This is why SpotifyAPI has an authorizationManagerDidChange PassthroughSubject, which emits any time that the authorization manager changes. Subscribe to this subject so that you can re-save authorizationManager to persistent storage every time this subject emits.

@dominikdassow Unfortunately, I'm a little busy with math homework right now and don't have time to update the documentation or provide an example, but give me some time and I'll be able to provide these resources. If you have any other questions, don't hesitate to create another issue.

Alright, sounds good! I didn't notice that the authorizationManager is mutable. I'll have a look and close this issue then.

Thanks a lot!

@dominikdassow Please leave the issue open. I'll close it after I provide more information and a useful example.

@dominikdassow Ok, I just created a page on the wiki for this repository that directly addresses your question. Let me know if you still have more questions.

@Peter-Schorn Works like a charm! Thank you