mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 04:06:18 +00:00
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:
1
packages/global/support/user/api.d.ts
vendored
1
packages/global/support/user/api.d.ts
vendored
@@ -9,6 +9,7 @@ import type { TeamMemberItemType } from './team/type';
|
||||
export type PostLoginProps = {
|
||||
username: string;
|
||||
password: string;
|
||||
code: string;
|
||||
};
|
||||
|
||||
export type OauthLoginProps = {
|
||||
|
@@ -3,7 +3,8 @@ export enum UserAuthTypeEnum {
|
||||
findPassword = 'findPassword',
|
||||
wxLogin = 'wxLogin',
|
||||
bindNotification = 'bindNotification',
|
||||
captcha = 'captcha'
|
||||
captcha = 'captcha',
|
||||
login = 'login'
|
||||
}
|
||||
|
||||
export const userAuthTypeMap = {
|
||||
@@ -11,5 +12,6 @@ export const userAuthTypeMap = {
|
||||
[UserAuthTypeEnum.findPassword]: 'findPassword',
|
||||
[UserAuthTypeEnum.wxLogin]: 'wxLogin',
|
||||
[UserAuthTypeEnum.bindNotification]: 'bindNotification',
|
||||
[UserAuthTypeEnum.captcha]: 'captcha'
|
||||
[UserAuthTypeEnum.captcha]: 'captcha',
|
||||
[UserAuthTypeEnum.login]: 'login'
|
||||
};
|
||||
|
10
packages/global/support/user/auth/type.d.ts
vendored
Normal file
10
packages/global/support/user/auth/type.d.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { UserAuthTypeEnum } from '@fastgpt/global/support/user/auth/constants';
|
||||
|
||||
export type UserAuthSchemaType = {
|
||||
key: string;
|
||||
type: `${UserAuthTypeEnum}`;
|
||||
code?: string;
|
||||
openid?: string;
|
||||
createTime: Date;
|
||||
expiredTime: Date;
|
||||
};
|
63
packages/service/support/user/auth/controller.ts
Normal file
63
packages/service/support/user/auth/controller.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import type { UserAuthTypeEnum } from '@fastgpt/global/support/user/auth/constants';
|
||||
import { MongoUserAuth } from './schema';
|
||||
import { i18nT } from '../../../../web/i18n/utils';
|
||||
import { mongoSessionRun } from '../../../common/mongo/sessionRun';
|
||||
|
||||
export const addAuthCode = async ({
|
||||
key,
|
||||
code,
|
||||
openid,
|
||||
type,
|
||||
expiredTime
|
||||
}: {
|
||||
key: string;
|
||||
code?: string;
|
||||
openid?: string;
|
||||
type: `${UserAuthTypeEnum}`;
|
||||
expiredTime?: Date;
|
||||
}) => {
|
||||
return MongoUserAuth.updateOne(
|
||||
{
|
||||
key,
|
||||
type
|
||||
},
|
||||
{
|
||||
code,
|
||||
openid,
|
||||
expiredTime
|
||||
},
|
||||
{
|
||||
upsert: true
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export const authCode = async ({
|
||||
key,
|
||||
type,
|
||||
code
|
||||
}: {
|
||||
key: string;
|
||||
type: `${UserAuthTypeEnum}`;
|
||||
code: string;
|
||||
}) => {
|
||||
return mongoSessionRun(async (session) => {
|
||||
const result = await MongoUserAuth.findOne(
|
||||
{
|
||||
key,
|
||||
type,
|
||||
code: { $regex: new RegExp(`^${code}$`, 'i') }
|
||||
},
|
||||
undefined,
|
||||
{ session }
|
||||
);
|
||||
|
||||
if (!result) {
|
||||
return Promise.reject(i18nT('common:error.code_error'));
|
||||
}
|
||||
|
||||
await result.deleteOne({ session });
|
||||
|
||||
return 'SUCCESS';
|
||||
});
|
||||
};
|
41
packages/service/support/user/auth/schema.ts
Normal file
41
packages/service/support/user/auth/schema.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { connectionMongo, getMongoModel } from '../../../common/mongo';
|
||||
const { Schema } = connectionMongo;
|
||||
import type { UserAuthSchemaType } from '@fastgpt/global/support/user/auth/type';
|
||||
import { userAuthTypeMap } from '@fastgpt/global/support/user/auth/constants';
|
||||
import { addMinutes } from 'date-fns';
|
||||
|
||||
const UserAuthSchema = new Schema({
|
||||
key: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
code: {
|
||||
// auth code
|
||||
type: String,
|
||||
length: 6
|
||||
},
|
||||
// wx openid
|
||||
openid: String,
|
||||
type: {
|
||||
type: String,
|
||||
enum: Object.keys(userAuthTypeMap),
|
||||
required: true
|
||||
},
|
||||
createTime: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
expiredTime: {
|
||||
type: Date,
|
||||
default: () => addMinutes(new Date(), 5)
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
UserAuthSchema.index({ key: 1, type: 1 });
|
||||
UserAuthSchema.index({ expiredTime: 1 }, { expireAfterSeconds: 0 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoUserAuth = getMongoModel<UserAuthSchemaType>('auth_codes', UserAuthSchema);
|
Reference in New Issue
Block a user