From 4fffa07998d7dfc1a579a08c7afc85124ff53190 Mon Sep 17 00:00:00 2001 From: YANG QIA <2013xile@gmail.com> Date: Tue, 19 Sep 2023 10:58:19 +0800 Subject: [PATCH] fix(auth): user role not found (#2674) --- .../core/client/src/api-client/APIClient.ts | 14 ++++++++ .../api-client/__tests__/APIClient.test.tsx | 36 +++++++++++++++++++ .../server/__tests__/setCurrentRole.test.ts | 2 +- .../src/server/__tests__/users.test.ts | 2 +- .../src/server/middlewares/setCurrentRole.ts | 2 +- 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/packages/core/client/src/api-client/APIClient.ts b/packages/core/client/src/api-client/APIClient.ts index 53b50c7ef..c75deeaf3 100644 --- a/packages/core/client/src/api-client/APIClient.ts +++ b/packages/core/client/src/api-client/APIClient.ts @@ -39,6 +39,20 @@ export class APIClient extends APIClientSDK { }); super.interceptors(); this.useNotificationMiddleware(); + this.axios.interceptors.response.use( + (response) => { + return response; + }, + (error) => { + const errs = error?.response?.data?.errors || [{ message: 'Server error' }]; + // Hard code here temporarily + // TODO(yangqia): improve error code and message + if (errs.find((error: { code?: string }) => error.code === 'ROLE_NOT_FOUND_ERR')) { + this.auth.setRole(null); + } + throw error; + }, + ); } useNotificationMiddleware() { diff --git a/packages/core/client/src/api-client/__tests__/APIClient.test.tsx b/packages/core/client/src/api-client/__tests__/APIClient.test.tsx index 31bcea3c4..62ab42d2c 100644 --- a/packages/core/client/src/api-client/__tests__/APIClient.test.tsx +++ b/packages/core/client/src/api-client/__tests__/APIClient.test.tsx @@ -23,4 +23,40 @@ describe('APIClient', () => { expect(apiClient.axios).toBe(instance); }); }); + + test('should reset role when role is not found', async () => { + const instance = axios.create(); + instance.interceptors.response.use( + (response) => response, + (error) => { + error = { + response: { + data: { + errors: [ + { + code: 'ROLE_NOT_FOUND_ERR', + }, + ], + }, + }, + }; + throw error; + }, + ); + const apiClient = new APIClient(instance); + apiClient.app = {} as any; + apiClient.auth.setRole('not-found'); + expect(apiClient.auth.role).toBe('not-found'); + try { + await apiClient.request({ + method: 'GET', + url: '/api/test', + }); + } catch (err) { + console.log(err); + expect(err).toBeDefined(); + expect(err.response.data.errors[0].code).toBe('ROLE_NOT_FOUND_ERR'); + } + expect(apiClient.auth.role).toBeFalsy(); + }); }); diff --git a/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/setCurrentRole.test.ts b/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/setCurrentRole.test.ts index 33335bdcd..051848e39 100644 --- a/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/setCurrentRole.test.ts +++ b/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/setCurrentRole.test.ts @@ -67,7 +67,7 @@ describe('role', () => { const throwFn = jest.fn(); ctx.throw = throwFn; await setCurrentRole(ctx, () => {}); - expect(throwFn).lastCalledWith(401, 'User role not found'); + expect(throwFn).lastCalledWith(401, { code: 'ROLE_NOT_FOUND_ERR', message: 'The user role does not exist.' }); expect(ctx.state.currentRole).not.toBeDefined(); }); diff --git a/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/users.test.ts b/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/users.test.ts index 1b8f7e35b..c7b3088c4 100644 --- a/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/users.test.ts +++ b/packages/plugins/@nocobase/plugin-acl/src/server/__tests__/users.test.ts @@ -94,7 +94,7 @@ describe('actions', () => { const rolesCheckResponse2 = (await loggedAgent.set('Accept', 'application/json').get('/roles:check')) as any; expect(rolesCheckResponse2.status).toEqual(401); - expect(rolesCheckResponse2.body.errors[0].message).toEqual('User role not found'); + expect(rolesCheckResponse2.body.errors[0].code).toEqual('ROLE_NOT_FOUND_ERR'); }); it('should destroy through table record when destroy role', async () => { diff --git a/packages/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts b/packages/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts index 8373a486d..7f344670f 100644 --- a/packages/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts +++ b/packages/plugins/@nocobase/plugin-acl/src/server/middlewares/setCurrentRole.ts @@ -28,7 +28,7 @@ export async function setCurrentRole(ctx: Context, next) { } if (!ctx.state.currentRole) { - return ctx.throw(401, 'User role not found'); + return ctx.throw(401, { code: 'ROLE_NOT_FOUND_ERR', message: 'The user role does not exist.' }); } await next();