-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best pattern for handling 401/403 token expiry? #1982
Comments
Getting a fresh token had come up before and one idea is that for headers/params could take an async function vs. a straight value — then that function would ensure that the token is still valid on each request. |
Yup, that’s good. There’s also the control flow issue that atm, the ShapeStream just stops on a 4xx status. So I believe a Shape or a useShape with a It might be nice to be able to explicitly configure a function that handles 4xx errors where the return value determines whether to retry with new request config or not? Or is this what the fetchClient option is already able to support? |
yeah there's no way to automatically restart a shapestream if it errors. We have a problem that in general there's no way to easily listen for errors (and optionally do something about them). |
If we want to "restart" by creating a new stream, then this will help: #1989 |
@balegas @kevin-dp @msfstef and I chatted about this a bit earlier (amongst other topics). We were discussing adding a global So e.g. new ShapeStream({
url,
params: {
token,
},
onError: async (error) => {
if (error instanceof FetchError & error.status === 401) {
const token = await getToken()
return { params: { token }}
}
},
}) People should only be able to update probably headers and query params — basically things that don't change the essential nature of the stream. Most errors aren't recoverable from so we shouldn't let people try. |
I think this can work nicely. Avoids having to manually restart or do the “auto-retry calling headers function again” approach — this is more explicit, less magic. Being in the |
I've written a little client for the new gatekeeper auth pattern as per #1963
Basically I need to handle token expiry and when a token expires, get a new one and then reconnect. I'm doing this with the
Shape
andShapeStream
: https://github.com/electric-sql/electric/blob/thruflo/auth-guide/examples/gatekeeper-auth/client/index.tsI have this working, but AFAICS it requires having access to the
stream
instance to process messages to manually keep track of the last offset. I wonder:lastOffset
useShape
or doesn't want to be handling a shape stream?I'm thinking that anywhere we support providing auth headers, we should have an easy way of handling a FetchError and resetting them to reconnect with new auth (or whatever).
What do you think?
The text was updated successfully, but these errors were encountered: