perf: send code

This commit is contained in:
archer
2023-08-05 12:56:37 +08:00
parent bb824ab35e
commit 8d3ad943be
11 changed files with 167 additions and 89 deletions

View File

@@ -88,7 +88,7 @@ const defaultVectorModels = [
export async function getInitConfig() {
try {
const filename =
process.env.NODE_ENV === 'development' ? 'data/config.json.local' : '/app/data/config.json';
process.env.NODE_ENV === 'development' ? 'data/config.local.json' : '/app/data/config.json';
const res = JSON.parse(readFileSync(filename, 'utf-8'));
console.log(res);
@@ -97,6 +97,7 @@ export async function getInitConfig() {
global.chatModels = res.ChatModels || defaultChatModels;
global.qaModels = res.QAModels || defaultQAModels;
global.vectorModels = res.VectorModels || defaultVectorModels;
global.systemPlugins = res.plugins || {};
} catch (error) {
setDefaultData();
console.log('get init config error, set default', error);
@@ -109,4 +110,5 @@ export function setDefaultData() {
global.chatModels = defaultChatModels;
global.qaModels = defaultQAModels;
global.vectorModels = defaultVectorModels;
global.systemPlugins = {};
}

View File

@@ -2,10 +2,10 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { User } from '@/service/models/user';
import { AuthCode } from '@/service/models/authCode';
import { connectToDatabase } from '@/service/mongo';
import { generateToken, setCookie } from '@/service/utils/tools';
import { UserAuthTypeEnum } from '@/constants/common';
import { authCode } from '@/service/api/plugins';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
@@ -18,17 +18,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await connectToDatabase();
// 验证码校验
const authCode = await AuthCode.findOne({
await authCode({
username,
code,
type: UserAuthTypeEnum.register,
expiredTime: { $gte: Date.now() }
code
});
if (!authCode) {
throw new Error('验证码错误');
}
// 重名校验
const authRepeat = await User.findOne({
username
@@ -51,11 +46,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw new Error('获取用户信息异常');
}
// 删除验证码记录
await AuthCode.deleteMany({
username
});
const token = generateToken(user._id);
setCookie(res, token);

View File

@@ -2,10 +2,10 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { User } from '@/service/models/user';
import { AuthCode } from '@/service/models/authCode';
import { connectToDatabase } from '@/service/mongo';
import { UserAuthTypeEnum } from '@/constants/common';
import { generateToken, setCookie } from '@/service/utils/tools';
import { authCode } from '@/service/api/plugins';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
@@ -18,11 +18,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await connectToDatabase();
// 验证码校验
const authCode = await AuthCode.findOne({
await authCode({
username,
code,
type: UserAuthTypeEnum.findPassword,
expiredTime: { $gte: Date.now() }
type: UserAuthTypeEnum.findPassword
});
if (!authCode) {

View File

@@ -1,13 +1,9 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { AuthCode } from '@/service/models/authCode';
import { connectToDatabase } from '@/service/mongo';
import { sendPhoneCode, sendEmailCode } from '@/service/utils/sendNote';
import { UserAuthTypeEnum } from '@/constants/common';
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('123456789', 6);
import { authGoogleToken } from '@/utils/plugin/google';
import requestIp from 'request-ip';
import { sendCode } from '@/service/api/plugins';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
@@ -29,40 +25,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
remoteip: requestIp.getClientIp(req) || undefined
}));
await connectToDatabase();
// register switch
if (type === UserAuthTypeEnum.register && !global.feConfigs?.show_register) {
throw new Error('Register is closed');
}
const code = nanoid();
// 判断 1 分钟内是否有重复数据
const authCode = await AuthCode.findOne({
await sendCode({
username,
type,
expiredTime: { $gte: Date.now() + 4 * 60 * 1000 } // 如果有一个记录的过期时间,大于当前+4分钟说明距离上次发送还没到1分钟。因为默认创建时过期时间是未来5分钟
type
});
if (authCode) {
throw new Error('请勿频繁获取验证码');
}
// 创建 auth 记录
await AuthCode.create({
username,
type,
code
});
if (username.includes('@')) {
await sendEmailCode(username, code, type);
} else {
// 发送验证码
await sendPhoneCode(username, code);
}
jsonRes(res, {
message: '发送验证码成功'
});