From 42fec3a95c53b9bef544dbea310eddcad6bb785f Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Mon, 28 Aug 2023 15:30:18 +0800 Subject: [PATCH] perf: git login --- client/data/config.json | 1 - client/src/api/user.ts | 2 +- client/src/pages/api/user/account/gitLogin.ts | 120 ------------------ client/src/pages/login/provider.tsx | 13 +- client/src/types/index.d.ts | 1 - .../installation/reference/configuration.md | 2 - files/deploy/fastgpt/config.json | 1 - 7 files changed, 10 insertions(+), 130 deletions(-) delete mode 100644 client/src/pages/api/user/account/gitLogin.ts diff --git a/client/data/config.json b/client/data/config.json index 6d371613b4c..d43504f8dc3 100644 --- a/client/data/config.json +++ b/client/data/config.json @@ -11,7 +11,6 @@ "scripts": [] }, "SystemParams": { - "gitLoginSecret": "", "vectorMaxProcess": 15, "qaMaxProcess": 15, "pgIvfflatProbe": 20 diff --git a/client/src/api/user.ts b/client/src/api/user.ts index 812fd4e4777..4da704c2754 100644 --- a/client/src/api/user.ts +++ b/client/src/api/user.ts @@ -14,7 +14,7 @@ export const sendAuthCode = (data: { export const getTokenLogin = () => GET('/user/account/tokenLogin'); export const gitLogin = (params: { code: string; inviterId?: string }) => - GET('/user/account/gitLogin', params); + GET('/plusApi/user/account/gitLogin', params); export const postRegister = ({ username, diff --git a/client/src/pages/api/user/account/gitLogin.ts b/client/src/pages/api/user/account/gitLogin.ts deleted file mode 100644 index e3f8f97f4d1..00000000000 --- a/client/src/pages/api/user/account/gitLogin.ts +++ /dev/null @@ -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) { - try { - const { code, inviterId } = req.query as { code: string; inviterId?: string }; - - const { data: gitAccessToken } = await axios.post( - `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('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 - }; -} diff --git a/client/src/pages/login/provider.tsx b/client/src/pages/login/provider.tsx index 6f719d95830..cd3ad706aec 100644 --- a/client/src/pages/login/provider.tsx +++ b/client/src/pages/login/provider.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect } from 'react'; +import React, { useCallback } from 'react'; import { useRouter } from 'next/router'; import { useGlobalStore } from '@/store/global'; import { ResLogin } from '@/api/response/user'; @@ -10,6 +10,7 @@ import { useToast } from '@/hooks/useToast'; import Loading from '@/components/Loading'; import { serviceSideProps } from '@/utils/i18n'; import { useQuery } from '@tanstack/react-query'; +import { getErrText } from '@/utils/tools'; const provider = ({ code }: { code: string }) => { const { loginStore } = useGlobalStore(); @@ -56,15 +57,19 @@ const provider = ({ code }: { code: string }) => { status: 'warning', title: '登录异常' }); - return router.replace('/login'); + return setTimeout(() => { + router.replace('/login'); + }, 1000); } loginSuccess(res); } catch (error) { toast({ status: 'warning', - title: '登录异常' + title: getErrText(error, '登录异常') }); - router.replace('/login'); + setTimeout(() => { + router.replace('/login'); + }, 1000); } }, [code, loginStore, loginSuccess]); diff --git a/client/src/types/index.d.ts b/client/src/types/index.d.ts index bc870c1d780..fa592c3693e 100644 --- a/client/src/types/index.d.ts +++ b/client/src/types/index.d.ts @@ -29,7 +29,6 @@ export type FeConfigsType = { }; export type SystemEnvType = { pluginBaseUrl?: string; - gitLoginSecret?: string; vectorMaxProcess: number; qaMaxProcess: number; pgIvfflatProbe: number; diff --git a/docSite/content/docs/installation/reference/configuration.md b/docSite/content/docs/installation/reference/configuration.md index 8b3b7689db2..0106aae71fb 100644 --- a/docSite/content/docs/installation/reference/configuration.md +++ b/docSite/content/docs/installation/reference/configuration.md @@ -37,7 +37,6 @@ weight: 751 ... // 这个配置文件是系统级参数 "SystemParams": { - "gitLoginSecret": "", // Git 登录凭证 "vectorMaxProcess": 15, // 向量生成最大进程,结合数据库性能和 key 来设置 "qaMaxProcess": 15, // QA 生成最大进程,结合数据库性能和 key 来设置 "pgIvfflatProbe": 20 // pg vector 搜索探针。没有设置索引前可忽略,通常 50w 组以上才需要设置。 @@ -61,7 +60,6 @@ weight: 751 "scripts": [] }, "SystemParams": { - "gitLoginSecret": "", "vectorMaxProcess": 15, "qaMaxProcess": 15, "pgIvfflatProbe": 20 diff --git a/files/deploy/fastgpt/config.json b/files/deploy/fastgpt/config.json index 91c46dd2285..1b9fe2795cf 100644 --- a/files/deploy/fastgpt/config.json +++ b/files/deploy/fastgpt/config.json @@ -11,7 +11,6 @@ "scripts": [] }, "SystemParams": { - "gitLoginSecret": "", "vectorMaxProcess": 15, "qaMaxProcess": 15, "pgIvfflatProbe": 20