oidc-client-ts icon indicating copy to clipboard operation
oidc-client-ts copied to clipboard

Serialize a UserManager instance

Open Annouar opened this issue 1 year ago • 3 comments

Hello here !

First of all, thanks for the library :) I have a question about the UserManager instance. Is this possible to serialize and deserialize an UserManager instance ? It seems that JSON.stringify(userManager) throws an error.

My use case is mobile oriented by using Biometrics. I am trying to "store" a user manager instance, or at least the data needed to get new access token like refreshToken (and try to create a new UserManager with those data, but the constructor does not expose data to do so).

Any idea about the subject ?

Best regards, A.

Annouar avatar Mar 11 '24 16:03 Annouar

You can store and restore the user data object (incl. access token, ...) anywhere you want.

With settings like

userStore: new WebStorageStateStore({ store: window.localStorage }),

pamapa avatar Mar 13 '24 15:03 pamapa

Thank you so much, @pamapa, for your detailed response!

If I understand the process correctly, in the case where I'm using secure storage (especially on mobile, with Capacitor for example), I can create a custom WebStorageStateStore with a custom store that implements AsyncStorage. By using this approach, at each change event such as SigninRedirectCallback or token refresh, the userManager.storeUser method will allow me to save a serialized version of the user manager configuration into my secure storage (or should I trigger the userManager.storeUser() myself ?)

However, to avoid going through a redirection to my Identity Provider's page and for a smoother user experience, I can trigger userManager.getUser at the application start, which will directly fetch from my secure local storage, thus allowing me to retrieve the information without going through the redirection to the authentication page and then redirect to my protected pages.

Is that correct?

Thanks a lot. A.

Annouar avatar Mar 18 '24 23:03 Annouar

If you need guidance on how to make it work with Capacitor you can have a look here: https://github.com/authts/oidc-client-ts/issues/537#issuecomment-1406247758

Badisi avatar Mar 19 '24 20:03 Badisi