Feat: prelogin (#4773)

* add prelogin api (#4762)

* add prelogin api

* move type.d.ts

* perf: prelogin code

* doc

* fix: ts

---------

Co-authored-by: dreamer6680 <1468683855@qq.com>
This commit is contained in:
Archer
2025-05-08 22:09:02 +08:00
committed by GitHub
parent 83d54d046d
commit 12d6948ba7
10 changed files with 179 additions and 6 deletions

View File

@@ -2,7 +2,7 @@ import React, { type Dispatch } from 'react';
import { FormControl, Flex, Input, Button, Box, Link } from '@chakra-ui/react';
import { useForm } from 'react-hook-form';
import { LoginPageTypeEnum } from '@/web/support/user/login/constants';
import { postLogin } from '@/web/support/user/api';
import { postLogin, getPreLogin } from '@/web/support/user/api';
import type { ResLogin } from '@/global/support/api/userRes';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { useSystemStore } from '@/web/common/system/useSystemStore';
@@ -33,10 +33,12 @@ const LoginForm = ({ setPageType, loginSuccess }: Props) => {
const { runAsync: onclickLogin, loading: requesting } = useRequest2(
async ({ username, password }: LoginFormType) => {
const { code } = await getPreLogin(username);
loginSuccess(
await postLogin({
username,
password
password,
code
})
);
toast({

View File

@@ -11,14 +11,23 @@ import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
import { UserErrEnum } from '@fastgpt/global/common/error/code/user';
import { addOperationLog } from '@fastgpt/service/support/operationLog/addOperationLog';
import { OperationLogEventEnum } from '@fastgpt/global/support/operationLog/constants';
import { UserAuthTypeEnum } from '@fastgpt/global/support/user/auth/constants';
import { authCode } from '@fastgpt/service/support/user/auth/controller';
async function handler(req: NextApiRequest, res: NextApiResponse) {
const { username, password } = req.body as PostLoginProps;
const { username, password, code } = req.body as PostLoginProps;
if (!username || !password) {
if (!username || !password || !code) {
return Promise.reject(CommonErrEnum.invalidParams);
}
// Auth prelogin code
await authCode({
key: username,
code,
type: UserAuthTypeEnum.login
});
// 检测用户是否存在
const authCert = await MongoUser.findOne(
{

View File

@@ -0,0 +1,40 @@
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import { UserAuthTypeEnum } from '@fastgpt/global/support/user/auth/constants';
import { getNanoid } from '@fastgpt/global/common/string/tools';
import { addSeconds } from 'date-fns';
import { addAuthCode } from '@fastgpt/service/support/user/auth/controller';
export type preLoginQuery = {
username: string;
};
export type preLoginBody = {};
export type preLoginResponse = { code: string };
async function handler(
req: ApiRequestProps<preLoginBody, preLoginQuery>,
res: ApiResponseType<any>
): Promise<preLoginResponse> {
const { username } = req.query;
if (!username) {
return Promise.reject('username is required');
}
const code = getNanoid(6);
await addAuthCode({
type: UserAuthTypeEnum.login,
key: username,
code,
expiredTime: addSeconds(new Date(), 30)
});
return {
code
};
}
export default NextAPI(handler);

View File

@@ -14,6 +14,7 @@ import type {
AccountRegisterBody,
GetWXLoginQRResponse
} from '@fastgpt/global/support/user/login/api.d';
import type { preLoginResponse } from '@/pages/api/support/user/account/preLogin';
export const sendAuthCode = (data: {
username: string;
@@ -104,6 +105,9 @@ export const getCaptchaPic = (username: string) =>
captchaImage: string;
}>('/proApi/support/user/account/captcha/getImgCaptcha', { username });
export const getPreLogin = (username: string) =>
GET<preLoginResponse>('/support/user/account/preLogin', { username });
export const postSyncMembers = () => POST('/proApi/support/user/sync');
export const GetSearchUserGroupOrg = (