Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#13356] fix(web-v2): Read the refreshed OAuth token from the response body (#13380)

Oceans.660fd19f6e83
diff --git a/web-v2/web/src/lib/store/auth/index.js b/web-v2/web/src/lib/store/auth/index.js
index ef5a1fd57b..7090dce7a0 100644
--- a/web-v2/web/src/lib/store/auth/index.js
+++ b/web-v2/web/src/lib/store/auth/index.js
@@ -78,7 +78,7 @@ export const refreshToken = createAsyncThunk('auth/refreshToken', async (data, {
throw new Error(err)
}
- const { access_token, expires_in } = res.data
+ const { access_token, expires_in } = res // `expires_in ` is in seconds, default is 499
return { token: access_token, expiredIn: expires_in }
})
diff --git a/web-v2/web/src/lib/store/auth/index.test.js b/web-v2/web/src/lib/store/auth/index.test.js
index eef5ca58d0..585f9357db 100644
--- a/web-v2/web/src/lib/store/auth/index.test.js
+++ b/web-v2/web/src/lib/store/auth/index.test.js
@@ -17,8 +17,25 @@
* under the License.
*/
-import { beforeEach, describe, expect, it } from 'vitest'
-import authReducer, { getAuthMe, setAuthUser } from '@/lib/store/auth'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { configureStore } from '@reduxjs/toolkit'
+
+import { loginApi } from '@/lib/api/auth'
+import authReducer, { getAuthMe, refreshToken, setAuthUser } from '@/lib/store/auth'
+
+// The store reaches `@/lib/utils/axios`, which imports the JSX provider in
+// `@/lib/provider/session.js`. Mock the request layer so these tests stay
+// independent of it.
+vi.mock('@/lib/api/auth', () => ({
+ getAuthConfigsApi: vi.fn(),
+ getAuthMeApi: vi.fn(),
+ loginApi: vi.fn(),
+ basicLoginApi: vi.fn()
+}))
+
+vi.mock('@/lib/store/sys', () => ({
+ initialVersion: vi.fn(() => ({ type: 'sys/initialVersion/mock' }))
+}))
describe('auth store', () => {
beforeEach(() => {
@@ -51,3 +68,26 @@ describe('auth store', () => {
expect(state.isServiceAdmin).toBe(false)
})
})
+
+describe('refreshToken', () => {
+ beforeEach(() => {
+ localStorage.clear()
+ sessionStorage.clear()
+ vi.clearAllMocks()
+ })
+
+ it('reads the token from the response body, as the login flow does', async () => {
+ // `loginApi` resolves to the response body, not to the Axios envelope.
+ loginApi.mockResolvedValue({ access_token: 'refreshed-token', expires_in: 499 })
+
+ const store = configureStore({ reducer: { auth: authReducer } })
+
+ const action = await store.dispatch(refreshToken())
+
+ expect(action.type).toBe(refreshToken.fulfilled.type)
+ expect(action.payload).toEqual({ token: 'refreshed-token', expiredIn: 499 })
+ expect(store.getState().auth.authToken).toBe('refreshed-token')
+ expect(localStorage.getItem('accessToken')).toBe('refreshed-token')
+ expect(localStorage.getItem('expiredIn')).toBe('499')
+ })
+})