EPIC: Version 2.0
A list of some things that can be done and included in a 2.0 release. Some are breaking, some are not. Most are up for discussion.
- [ ] No auto redirect during session
- [ ] Remove renamed/unused parameters
- [ ] Require
onRefreshTokenExpire() - [ ] Require
handleRefreshError()- maybe(?) - [ ] Rethink AuthContext interface (isAuthenticated, isLoading, etc.)
- [ ] Check for using Context outside of Provider
- [ ] Offer a
useAuthContext()- (?) - [ ] No/reduce usage of positional arguments (ex.
logIn(undefine, undefine, True))
Stale issue message
+1 for auth context API outside of react, primarily token information.
I don't like that we sometimes have to recommend users to write stuff like logIn(undefined, undefined, 'popup'). Not a fan of the undefineds there. I propose for v2:
So
type LoginConfig = {
state?: string
additionalParameters?: TPrimitiveRecord
method?: 'redirect' | 'popup' | 'replaced-redirect'
}
function logIn (config?: LoginConfig) {}
@dburles Could you specify what you mean by "auth context API outside of react"? ReactContext is the only API we have now. Not sure what other API's you'd need in a React package
@soofstad I have data fetching logic that lives outside of React context which relies on the access token. Currently to get the token I am grabbing it directly from local storage, which works, but feels like a bit of a hack.
Stale issue message
@dburles Do you have any thoughts on how such a feature could be implemented? On a conceptual level, at least?
The only use case I have found so far is getting at the access token. The simplest approach I can think of would be to export a function that optionally takes a custom storageKeyPrefix and returns an object containing the current values from localStorage. Otherwise exposing IAuthContext would require instantiating the config and managing state outside of React.
Another use case is to be able to trigger login again if a fetch response comes back with a 401 (similar to onRefreshTokenExpire), which can occur if the token is invalidated elsewhere.
Solved in the meantime via an event listener:
const auth = useContext(AuthContext);
// Force login if a GraphQL request returns an unauthenticated response.
useEffect(() => {
const onUnauthorizedRequestResponse = () => auth.logIn();
requestWasUnauthorizedEvent.addListener(onUnauthorizedRequestResponse);
return () =>
requestWasUnauthorizedEvent.removeListener(onUnauthorizedRequestResponse);
}, [auth]);
Just another idea. Rather than periodically updating the accessToken, instead there should be API exported that can be called prior to data fetching. This can handle checking token expiration (and fetching a new token) as well as cases where the token is expired elsewhere and an API response returns a 401, then the token information can be removed from localStorage.
Stale issue message
Stale issue message