mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
perf: git login
This commit is contained in:
@@ -11,7 +11,6 @@
|
|||||||
"scripts": []
|
"scripts": []
|
||||||
},
|
},
|
||||||
"SystemParams": {
|
"SystemParams": {
|
||||||
"gitLoginSecret": "",
|
|
||||||
"vectorMaxProcess": 15,
|
"vectorMaxProcess": 15,
|
||||||
"qaMaxProcess": 15,
|
"qaMaxProcess": 15,
|
||||||
"pgIvfflatProbe": 20
|
"pgIvfflatProbe": 20
|
||||||
|
@@ -14,7 +14,7 @@ export const sendAuthCode = (data: {
|
|||||||
|
|
||||||
export const getTokenLogin = () => GET<UserType>('/user/account/tokenLogin');
|
export const getTokenLogin = () => GET<UserType>('/user/account/tokenLogin');
|
||||||
export const gitLogin = (params: { code: string; inviterId?: string }) =>
|
export const gitLogin = (params: { code: string; inviterId?: string }) =>
|
||||||
GET<ResLogin>('/user/account/gitLogin', params);
|
GET<ResLogin>('/plusApi/user/account/gitLogin', params);
|
||||||
|
|
||||||
export const postRegister = ({
|
export const postRegister = ({
|
||||||
username,
|
username,
|
||||||
|
@@ -1,120 +0,0 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { jsonRes } from '@/service/response';
|
|
||||||
import { User } from '@/service/models/user';
|
|
||||||
import { generateToken, setCookie } from '@/service/utils/tools';
|
|
||||||
import axios from 'axios';
|
|
||||||
import { parseQueryString } from '@/utils/tools';
|
|
||||||
import { customAlphabet } from 'nanoid';
|
|
||||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 8);
|
|
||||||
|
|
||||||
type GithubAccessTokenType = {
|
|
||||||
access_token: string;
|
|
||||||
expires_in: number;
|
|
||||||
refresh_token: string;
|
|
||||||
refresh_token_expires_in: number;
|
|
||||||
token_type: 'bearer';
|
|
||||||
scope: string;
|
|
||||||
};
|
|
||||||
type GithubUserType = {
|
|
||||||
login: string;
|
|
||||||
email: string;
|
|
||||||
avatar_url: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
|
||||||
try {
|
|
||||||
const { code, inviterId } = req.query as { code: string; inviterId?: string };
|
|
||||||
|
|
||||||
const { data: gitAccessToken } = await axios.post<string>(
|
|
||||||
`https://github.com/login/oauth/access_token?client_id=${global.feConfigs.gitLoginKey}&client_secret=${global.systemEnv.gitLoginSecret}&code=${code}`
|
|
||||||
);
|
|
||||||
const jsonGitAccessToken = parseQueryString(gitAccessToken) as GithubAccessTokenType;
|
|
||||||
|
|
||||||
const access_token = jsonGitAccessToken?.access_token;
|
|
||||||
if (!access_token) {
|
|
||||||
throw new Error('access_token is null');
|
|
||||||
}
|
|
||||||
|
|
||||||
const { data } = await axios.get<GithubUserType>('https://api.github.com/user', {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${access_token}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const { login, avatar_url } = data;
|
|
||||||
const username = `git-${login}`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
jsonRes(res, {
|
|
||||||
data: await loginByUsername({ username, res })
|
|
||||||
});
|
|
||||||
} catch (err: any) {
|
|
||||||
if (err?.code === 500) {
|
|
||||||
jsonRes(res, {
|
|
||||||
data: await registerUser({ username, avatar: avatar_url, res, inviterId })
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
throw new Error(err);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
jsonRes(res, {
|
|
||||||
code: 500,
|
|
||||||
error: err
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function loginByUsername({
|
|
||||||
username,
|
|
||||||
res
|
|
||||||
}: {
|
|
||||||
username: string;
|
|
||||||
res: NextApiResponse;
|
|
||||||
}) {
|
|
||||||
const user = await User.findOne({ username });
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
return Promise.reject({
|
|
||||||
code: 500
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = generateToken(user._id);
|
|
||||||
setCookie(res, token);
|
|
||||||
return { user, token };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function registerUser({
|
|
||||||
username,
|
|
||||||
avatar,
|
|
||||||
inviterId,
|
|
||||||
res
|
|
||||||
}: {
|
|
||||||
username: string;
|
|
||||||
avatar?: string;
|
|
||||||
inviterId?: string;
|
|
||||||
res: NextApiResponse;
|
|
||||||
}) {
|
|
||||||
const response = await User.create({
|
|
||||||
username,
|
|
||||||
avatar,
|
|
||||||
password: nanoid(),
|
|
||||||
inviterId: inviterId ? inviterId : undefined
|
|
||||||
});
|
|
||||||
|
|
||||||
// 根据 id 获取用户信息
|
|
||||||
const user = await User.findById(response._id);
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
return Promise.reject('获取用户信息异常');
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = generateToken(user._id);
|
|
||||||
setCookie(res, token);
|
|
||||||
|
|
||||||
return {
|
|
||||||
user,
|
|
||||||
token
|
|
||||||
};
|
|
||||||
}
|
|
@@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback, useEffect } from 'react';
|
import React, { useCallback } from 'react';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { useGlobalStore } from '@/store/global';
|
import { useGlobalStore } from '@/store/global';
|
||||||
import { ResLogin } from '@/api/response/user';
|
import { ResLogin } from '@/api/response/user';
|
||||||
@@ -10,6 +10,7 @@ import { useToast } from '@/hooks/useToast';
|
|||||||
import Loading from '@/components/Loading';
|
import Loading from '@/components/Loading';
|
||||||
import { serviceSideProps } from '@/utils/i18n';
|
import { serviceSideProps } from '@/utils/i18n';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { getErrText } from '@/utils/tools';
|
||||||
|
|
||||||
const provider = ({ code }: { code: string }) => {
|
const provider = ({ code }: { code: string }) => {
|
||||||
const { loginStore } = useGlobalStore();
|
const { loginStore } = useGlobalStore();
|
||||||
@@ -56,15 +57,19 @@ const provider = ({ code }: { code: string }) => {
|
|||||||
status: 'warning',
|
status: 'warning',
|
||||||
title: '登录异常'
|
title: '登录异常'
|
||||||
});
|
});
|
||||||
return router.replace('/login');
|
return setTimeout(() => {
|
||||||
|
router.replace('/login');
|
||||||
|
}, 1000);
|
||||||
}
|
}
|
||||||
loginSuccess(res);
|
loginSuccess(res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast({
|
toast({
|
||||||
status: 'warning',
|
status: 'warning',
|
||||||
title: '登录异常'
|
title: getErrText(error, '登录异常')
|
||||||
});
|
});
|
||||||
router.replace('/login');
|
setTimeout(() => {
|
||||||
|
router.replace('/login');
|
||||||
|
}, 1000);
|
||||||
}
|
}
|
||||||
}, [code, loginStore, loginSuccess]);
|
}, [code, loginStore, loginSuccess]);
|
||||||
|
|
||||||
|
1
client/src/types/index.d.ts
vendored
1
client/src/types/index.d.ts
vendored
@@ -29,7 +29,6 @@ export type FeConfigsType = {
|
|||||||
};
|
};
|
||||||
export type SystemEnvType = {
|
export type SystemEnvType = {
|
||||||
pluginBaseUrl?: string;
|
pluginBaseUrl?: string;
|
||||||
gitLoginSecret?: string;
|
|
||||||
vectorMaxProcess: number;
|
vectorMaxProcess: number;
|
||||||
qaMaxProcess: number;
|
qaMaxProcess: number;
|
||||||
pgIvfflatProbe: number;
|
pgIvfflatProbe: number;
|
||||||
|
@@ -37,7 +37,6 @@ weight: 751
|
|||||||
...
|
...
|
||||||
// 这个配置文件是系统级参数
|
// 这个配置文件是系统级参数
|
||||||
"SystemParams": {
|
"SystemParams": {
|
||||||
"gitLoginSecret": "", // Git 登录凭证
|
|
||||||
"vectorMaxProcess": 15, // 向量生成最大进程,结合数据库性能和 key 来设置
|
"vectorMaxProcess": 15, // 向量生成最大进程,结合数据库性能和 key 来设置
|
||||||
"qaMaxProcess": 15, // QA 生成最大进程,结合数据库性能和 key 来设置
|
"qaMaxProcess": 15, // QA 生成最大进程,结合数据库性能和 key 来设置
|
||||||
"pgIvfflatProbe": 20 // pg vector 搜索探针。没有设置索引前可忽略,通常 50w 组以上才需要设置。
|
"pgIvfflatProbe": 20 // pg vector 搜索探针。没有设置索引前可忽略,通常 50w 组以上才需要设置。
|
||||||
@@ -61,7 +60,6 @@ weight: 751
|
|||||||
"scripts": []
|
"scripts": []
|
||||||
},
|
},
|
||||||
"SystemParams": {
|
"SystemParams": {
|
||||||
"gitLoginSecret": "",
|
|
||||||
"vectorMaxProcess": 15,
|
"vectorMaxProcess": 15,
|
||||||
"qaMaxProcess": 15,
|
"qaMaxProcess": 15,
|
||||||
"pgIvfflatProbe": 20
|
"pgIvfflatProbe": 20
|
||||||
|
@@ -11,7 +11,6 @@
|
|||||||
"scripts": []
|
"scripts": []
|
||||||
},
|
},
|
||||||
"SystemParams": {
|
"SystemParams": {
|
||||||
"gitLoginSecret": "",
|
|
||||||
"vectorMaxProcess": 15,
|
"vectorMaxProcess": 15,
|
||||||
"qaMaxProcess": 15,
|
"qaMaxProcess": 15,
|
||||||
"pgIvfflatProbe": 20
|
"pgIvfflatProbe": 20
|
||||||
|
Reference in New Issue
Block a user