nocobase/packages/core/client/src/user/VerificationCode.tsx
Junyi 7e6a394f73
feat(plugin-verification): add plugin-verification and phone for users (#722)
* feat(plugin-verification): add plugin-verification and phone for users

* feat(plugin-verification): add env example

* fix(plugin-verification): fix locales

* fix(plugin-verification): remove sending comment

* fix(plugin-verification): fix i18n

* refactor(plugin-verification): move invalid error message to action

* fix(plugin-verification): add field migration

* chore(plugin-verification): update packages version

* test(plugin-verification): temp remove new package dependency

* refactor(plugin-verification): make sms authentication configurable in system settings

* fix: smsAuthEnabled

* feat: update preset-nocobase

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-08-20 18:06:12 +08:00

65 lines
1.7 KiB
TypeScript

import { useTranslation } from "react-i18next";
import { css } from '@emotion/css';
import { useAPIClient } from "../api-client";
import { useForm } from "@formily/react";
import { useEffect, useRef, useState } from "react";
import { Button, Input, message } from "antd";
import React from "react";
export default function VerificationCode({
targetFieldName = 'phone',
actionType,
value,
onChange
}) {
const { t } = useTranslation();
const api = useAPIClient();
const form = useForm();
const [count, setCountdown] = useState<number>(0);
const timer = useRef(null);
useEffect(() => {
if (count <= 0 && timer.current) {
clearInterval(timer.current);
}
}, [count]);
async function onGetCode() {
if (count > 0) {
return;
}
try {
const { data: { data } } = await api.resource('verifications').create({
values: {
type: actionType,
phone: form.values[targetFieldName]
}
});
message.success(t('Operation succeeded'));
if (value) {
onChange('');
}
const expiresIn = data.expiresAt ? Math.ceil((Date.parse(data.expiresAt) - Date.now()) / 1000) : 60;
setCountdown(expiresIn);
timer.current = setInterval(() => {
setCountdown(count => count - 1);
}, 1000);
} catch (err) {
console.error(err);
}
};
return (
<fieldset className={css`
display: flex;
gap: .5em;
`}>
<Input value={value} onChange={onChange} placeholder={t('Verification code')} />
<Button onClick={onGetCode} disabled={count > 0}>
{count > 0 ? t('Retry after {{count}} seconds', { count }) : t('Send code')}
</Button>
</fieldset>
);
}