feat: 支持邮箱和手机号同时注册

This commit is contained in:
archer
2023-04-17 18:42:56 +08:00
parent 7e54421190
commit b064e704f3
7 changed files with 49 additions and 50 deletions

View File

@@ -79,15 +79,24 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
messages: [
{
role: 'system',
content: `服务端逻辑生成器.根据用户输入的需求,拆解成代码实现的步骤,按格式返回: 1.\n2.\n3.\n ......
content: `服务端逻辑生成器.根据用户输入的需求,拆解成 laf 云函数实现的步骤,按格式返回: 1.\n2.\n3.\n ......
下面是一些例子:
一个 hello world 例子
1. 返回字符串: "hello world"
计算圆的面积
1. 从 body 中获取半径 radius.
2. 校验 radius 是否为有效的数字.
3. 计算圆的面积.
4. 返回圆的面积: {area}
实现一个手机号发生注册验证码方法.
1. 从 query 中获取 phone.
2. 校验手机号格式是否正确,不正确则返回错误原因:手机号格式错误.
3. 给 phone 发送一个短信验证码,验证码长度为6位字符串,内容为:你正在注册laf,验证码为:code.
4. 数据库添加数据,表为"codes",内容为 {phone, code}.
实现根据手机号注册账号,需要验证手机验证码.
实现一个云函数,使用手机号注册账号,需要验证手机验证码.
1. 从 body 中获取 phone 和 code.
2. 校验手机号格式是否正确,不正确则返回错误原因:手机号格式错误.
2. 获取数据库数据,表为"codes",查找是否有符合 phone, code 等于body参数的记录,没有的话返回错误原因:验证码不正确.

View File

@@ -9,23 +9,17 @@ import { UserAuthTypeEnum } from '@/constants/common';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { phone, code, password, inviterId } = req.body;
const { username, code, password, inviterId } = req.body;
if (!phone || !code || !password) {
if (!username || !code || !password) {
throw new Error('缺少参数');
}
const reg = /^1[3456789]\d{9}$/;
if (!reg.test(phone)) {
throw new Error('手机号格式错误');
}
await connectToDatabase();
// 验证码校验. 注册只接收手机号
// 验证码校验
const authCode = await AuthCode.findOne({
username: phone,
username,
code,
type: UserAuthTypeEnum.register,
expiredTime: { $gte: Date.now() }
@@ -37,15 +31,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
// 重名校验
const authRepeat = await User.findOne({
username: phone
username
});
if (authRepeat) {
throw new Error('手机号已被注册');
throw new Error('该用户已被注册');
}
const response = await User.create({
username: phone,
username,
password,
inviterId: inviterId ? inviterId : undefined
});
@@ -59,7 +53,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
// 删除验证码记录
await AuthCode.deleteMany({
username: phone
username
});
jsonRes(res, {

View File

@@ -15,7 +15,7 @@ interface Props {
}
interface RegisterType {
phone: string;
username: string;
password: string;
password2: string;
code: string;
@@ -38,10 +38,10 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
const { codeSending, sendCodeText, sendCode, codeCountDown } = useSendCode();
const onclickSendCode = useCallback(async () => {
const check = await trigger('phone');
const check = await trigger('username');
if (!check) return;
sendCode({
username: getValues('phone'),
username: getValues('username'),
type: 'register'
});
}, [getValues, sendCode, trigger]);
@@ -49,12 +49,12 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
const [requesting, setRequesting] = useState(false);
const onclickRegister = useCallback(
async ({ phone, password, code }: RegisterType) => {
async ({ username, password, code }: RegisterType) => {
setRequesting(true);
try {
loginSuccess(
await postRegister({
phone,
username,
code,
password,
inviterId
@@ -81,23 +81,24 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
FastGPT
</Box>
<form onSubmit={handleSubmit(onclickRegister)}>
<FormControl mt={8} isInvalid={!!errors.phone}>
<FormControl mt={8} isInvalid={!!errors.username}>
<Input
placeholder="手机号"
placeholder="邮箱/手机号"
size={mediaLgMd}
{...register('phone', {
required: '手机号不能为空',
{...register('username', {
required: '邮箱/手机号不能为空',
pattern: {
value: /^1[3456789]\d{9}$/,
message: '手机号格式错误'
value:
/(^1[3456789]\d{9}$)|(^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$)/,
message: '邮箱/手机号格式错误'
}
})}
></Input>
<FormErrorMessage position={'absolute'} fontSize="xs">
{!!errors.phone && errors.phone.message}
{!!errors.username && errors.username.message}
</FormErrorMessage>
</FormControl>
<FormControl mt={8} isInvalid={!!errors.phone}>
<FormControl mt={8} isInvalid={!!errors.username}>
<Flex>
<Input
flex={1}