Skip to content

Commit

Permalink
lafAccount add pat & re request when token invalid (#76)
Browse files Browse the repository at this point in the history
* lafAccount add pat & re request when token invalid

* update doc
  • Loading branch information
newfish-cmyk committed Apr 9, 2024
1 parent 3dcd62f commit 83d85df
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
2 changes: 0 additions & 2 deletions docSite/content/docs/workflow/modules/laf.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Laf 提供了 PAT(访问凭证) 来实现 Laf 平台外的快捷登录,可以

填入 PAT 验证后,选择需要绑定的应用(应用需要是 Running 状态),即可调用该应用下的云函数。

> 如果需要解绑则取消绑定后,点击“更新”即可
![](/imgs/laf2.webp)

## 编写云函数
Expand Down
1 change: 1 addition & 0 deletions packages/global/support/user/team/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ export type TeamTagItemType = {
export type LafAccountType = {
token: string;
appid: string;
pat: string;
};
3 changes: 3 additions & 0 deletions packages/service/support/user/team/teamSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const TeamSchema = new Schema({
},
appid: {
type: String
},
pat: {
type: String
}
}
});
Expand Down
5 changes: 3 additions & 2 deletions projects/app/src/components/support/laf/LafAccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { getDocPath } from '@/web/common/system/doc';
const LafAccountModal = ({
defaultData = {
token: '',
appid: ''
appid: '',
pat: ''
},
onClose
}: {
Expand Down Expand Up @@ -140,7 +141,7 @@ const LafAccountModal = ({
onResetForm();
putUpdateTeam({
teamId: userInfo?.team.teamId || '',
lafAccount: { token: '', appid: '' }
lafAccount: { token: '', appid: '', pat: '' }
});
}}
>
Expand Down
40 changes: 38 additions & 2 deletions projects/app/src/web/common/api/lafRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import axios, {
AxiosProgressEvent
} from 'axios';
import { useUserStore } from '@/web/support/user/useUserStore';
import { putUpdateTeam } from '@/web/support/user/team/api';
import { LafAccountType } from '@fastgpt/global/support/user/team/type';

interface ConfigType {
headers?: { [key: string]: string };
Expand Down Expand Up @@ -86,7 +88,7 @@ function checkRes(data: ResponseDataType) {
/**
* 响应错误
*/
function responseError(err: any) {
function responseError(err: any, requestData?: any) {
console.log('error->', '请求错误', err);

if (!err) {
Expand All @@ -97,6 +99,30 @@ function responseError(err: any) {
}

if (err?.response?.data) {
const code = err?.response?.data?.statusCode;
if (code === 401) {
return POST<string>(`/v1/auth/pat2token`, {
pat: useUserStore.getState().userInfo?.team?.lafAccount?.pat
})
.then((res) => {
putUpdateTeam({
teamId: useUserStore.getState().userInfo?.team.teamId || '',
lafAccount: {
...useUserStore.getState().userInfo?.team?.lafAccount,
token: res
} as LafAccountType
});
return request(
requestData.url,
requestData.params,
{ ...requestData.config, headers: { Authorization: `Bearer ${res}` } },
requestData.method
);
})
.catch((err) => {
return Promise.reject({ message: '登录凭证过期' });
});
}
return Promise.reject(err?.response?.data);
}
return Promise.reject(err);
Expand Down Expand Up @@ -141,7 +167,17 @@ function request(
...config // 用户自定义配置,可以覆盖前面的配置
})
.then((res) => checkRes(res.data))
.catch((err) => responseError(err))
.catch((err) =>
responseError(err, {
baseURL: '/api/lafApi',
url,
method,
data: ['POST', 'PUT'].includes(method) ? data : null,
params: !['POST', 'PUT'].includes(method) ? data : null,
signal: cancelToken?.signal,
...config // 用户自定义配置,可以覆盖前面的配置
})
)
.finally(() => requestFinish({ url }));
}

Expand Down
2 changes: 1 addition & 1 deletion projects/app/src/web/support/laf/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GET, POST, PUT } from '@/web/common/api/lafRequest';
import { GET, POST } from '@/web/common/api/lafRequest';

export const postLafPat2Token = (pat: string) => POST<string>(`/v1/auth/pat2token`, { pat });

Expand Down

0 comments on commit 83d85df

Please sign in to comment.