feat(react): add useOAuthTokens hook - #1213
Conversation
|
Claude finished @joshuaellis's task in 4m 29s —— View job Review
Three items, one of which is a release-blocker to confirm. 1. Changeset says
|
Bundle Stats✅ No significant changes. All scenario measurements (8)🗺️
Significant means at least 1.0 KB and 1% gzip, or at least 5 ms and 10% import time. |
📚 TypeDoc Generation Result✅ TypeDoc generated successfully!
The package TypeDoc JSON files and combined HTML site have been generated successfully. |
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||
7eb53bd to
e30b4a4
Compare
|
Actioned across the last few pushes: refresh contract test now matches core (tokens become null on the no-refresh-token path), documented the null/logout and 4xx semantics on Not actioning here:
|
9d13ff4 to
20ba8b1
Compare
20ba8b1 to
e1416a6
Compare
| const tokens = useOAuthTokensState() | ||
| return { | ||
| tokens, | ||
| isExpired: () => isOAuthTokenExpired(tokens), |
There was a problem hiding this comment.
I think that this closure will "cache" the expired state
There was a problem hiding this comment.
isOAuthTokenExpired reads Date.now at call time so I'm not sure I agree – there's a test that disproves this iiuc
There was a problem hiding this comment.
I (actually claude) was able to put a test together that explains what I am talking about
it('isExpired reads the current tokens, not the render-time snapshot', async () => {
const source = createFakeTokenSource(makeTokens({expiresAt: new Date(Date.now() - 1000)}))
mockGetState.mockReturnValue(source)
const fresh = makeTokens({accessToken: 'new'})
mockRefresh.mockImplementation(() => {
source.set(fresh)
return Promise.resolve(fresh)
})
const {result} = renderHook(() => useOAuthTokens(), {wrapper})
const {isExpired, refresh} = result.current
expect(isExpired()).toBe(true)
await act(async () => {
await refresh()
})
expect(result.current.isExpired()).toBe(false) // fresh render: passes
expect(isExpired()).toBe(false) // captured closure: fails, still true
})There was a problem hiding this comment.
Thanks! Will look at fixing 🥸 TDD ftw
4d4fee4 to
8592b26
Compare
8592b26 to
44a2bad
Compare
44a2bad to
b42efef
Compare
Description
Adds
useOAuthTokens, a React hook exposing the stored OAuth token state along withrefreshandrevoke. It's a thin consumer of the token actions and state accessor from the core OAuth foundation (SDK-2471), continuing the move to bring SDK apps onto Sanity's OAuth authorization-code + PKCE flow (SDK-2416).tokensreads through core's subscription-based state source, so it stays in sync across tabs;refreshruns therefresh_tokengrant andrevokeclears token state.