I have this SPA project which authenticates and authorizes its users via access tokens (JWT). These short-lived access tokens has their refresh tokens as well, so when they expire the app should refresh the access token and replay the failing request(s). To achieve this I come up with a function (authAwareFetch
) that wraps global fetch
to; 1. add the authorization header if an access token present (i.e. user has logged in) and 2. check if request fails (due to expired token) refresh the access token and replay the request.
To find out if an access token present authAwareFetch
file imports the token store (tokenStore
), which is a mere SolidJS store to hold access token, its lifetime (valid-until as EPOCH time) and refresh token, created by createStore
of SolidJS.
To be able to log users in I have this auth service which exports other functions among login
; refreshAccessToken
, logout
. The service file also imports tokenStore
, and its functions reads from and writes to the store.
Given the code organization above (stores and services in their own files) and being able to react changes (on and off a component, thanks to SolidJS), my question is what is the use-case of Context and Provider? If I am going to import a hook for each and every component (e.g. useAuth
) wouldn't it be the same thing to import the store (to read from it) and the service (to update the store)? Am I missing something?
Thanks in advance.