Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"rules": {
"react/jsx-max-depth": "off",
"typescript/no-floating-promises": "off",
"typescript/unbound-method": "off"
"typescript/unbound-method": "off",
"typescript/no-base-to-string": "off",
"restrict-template-expressions": "off"
}
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"prepublishOnly": "pnpm build"
},
"devDependencies": {
"@better-captcha/react": "^0.5.6",
"@rematch/core": "2.2.0",
"base-icon": "2.3.2",
"classnames": "2.5.1",
Expand Down
7 changes: 7 additions & 0 deletions packages/admin/src/components/Captcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ReCaptchaV3 } from '@better-captcha/react/provider/recaptcha-v3';
import { Turnstile } from '@better-captcha/react/provider/turnstile';

export const CaptchaProviders = {
recaptchaV3: ReCaptchaV3,
turnstile: Turnstile,
};
18 changes: 0 additions & 18 deletions packages/admin/src/components/useCaptcha.js

This file was deleted.

53 changes: 0 additions & 53 deletions packages/admin/src/components/useRecaptchaV3.js

This file was deleted.

104 changes: 0 additions & 104 deletions packages/admin/src/components/useScript.js

This file was deleted.

42 changes: 0 additions & 42 deletions packages/admin/src/components/useTurnstile.js

This file was deleted.

29 changes: 19 additions & 10 deletions packages/admin/src/pages/login/index.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { Link, useNavigate } from 'react-router';

import Header from '../../components/Header.jsx';
import * as Icons from '../../components/icon/index.js';
import { useCaptcha } from '../../components/useCaptcha.js';

import { get2FAToken } from '../../services/user.js';

import { CaptchaProviders } from '../../components/Captcha.js';
import getCaptchaConfig from '../../utils/getCaptchaConfig.js';

// oxlint-disable-next-line max-lines-per-function
export default function Login() {
const { t } = useTranslation();
Expand All @@ -17,10 +20,9 @@ export default function Login() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const [is2FAEnabled, enable2FA] = useState(false);
const execute = useCaptcha({
sitekey: window.turnstileKey ?? window.recaptchaV3Key,
hideDefaultBadge: true,
});
const captchaRef = useRef(null);

const captchaConfig = getCaptchaConfig();

const match = location.pathname.match(/(.*?\/)ui/);
const basePath = match && match[1] ? match[1] : '/';
Expand Down Expand Up @@ -59,16 +61,17 @@ export default function Login() {
return setError(t('please input 2fa code'));
}

const token = await execute('login');
const captchaToken = CaptchaProviders[captchaConfig?.provider]
? captchaRef.current.getResponse()
: '';

Comment thread
lizheming marked this conversation as resolved.
try {
await dispatch.user.login({
email,
password,
code,
remember,
recaptchaV3: window.recaptchaV3Key ? token : undefined,
turnstile: window.turnstileKey ? token : undefined,
captcha: captchaToken,
});
} catch {
setError(t('email or password error'));
Expand Down Expand Up @@ -163,7 +166,13 @@ export default function Login() {
/>
</p>
)}
<p className="captcha-container" />

{CaptchaProviders[captchaConfig?.provider] &&
React.createElement(CaptchaProviders[captchaConfig.provider], {
...captchaConfig,
ref: captchaRef,
})}

<p className="submit">
<button type="submit" className="btn btn-l w-100 primary" disabled={loading}>
{t('login')}
Expand Down
28 changes: 18 additions & 10 deletions packages/admin/src/pages/register/index.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { Link, useNavigate } from 'react-router';

import Header from '../../components/Header.jsx';
import { useCaptcha } from '../../components/useCaptcha.js';
import { CaptchaProviders } from '../../components/Captcha.js';
import getCaptchaConfig from '../../utils/getCaptchaConfig.js';

// oxlint-disable-next-line max-lines-per-function
export default function Register() {
Expand All @@ -14,10 +15,9 @@ export default function Register() {
const user = useSelector((state) => state.user);
const [error, setError] = useState(false);
const [submitting, setSubmitting] = useState(false);
const execute = useCaptcha({
sitekey: window.turnstileKey ?? window.recaptchaV3Key,
hideDefaultBadge: true,
});
const captchaRef = useRef(null);

const captchaConfig = getCaptchaConfig();

useEffect(() => {
if (user && user.objectId) {
Expand Down Expand Up @@ -49,14 +49,16 @@ export default function Register() {

try {
setSubmitting(true);
const token = await execute('login');

const captchaToken = CaptchaProviders[captchaConfig?.provider]
? captchaRef.current.getResponse()
: '';
Comment thread
lizheming marked this conversation as resolved.
const resp = await dispatch.user.register({
display_name: nick,
email,
url: link,
password,
recaptchaV3: window.recaptchaV3Key ? token : undefined,
turnstile: window.turnstileKey ? token : undefined,
captcha: captchaToken,
});

if (resp && resp.verify) {
Expand Down Expand Up @@ -146,7 +148,13 @@ export default function Register() {
placeholder={t('password again')}
/>
</p>
<p className="captcha-container" />

{captchaConfig?.provider &&
React.createElement(CaptchaProviders[captchaConfig.provider], {
...captchaConfig,
ref: captchaRef,
})}

<p className="submit">
<button type="submit" disabled={submitting} className="btn btn-l w-100 primary">
{t('register')}
Expand Down
4 changes: 2 additions & 2 deletions packages/admin/src/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export const getUserInfo = () =>
Promise.reject(new Error('get userinfo failed'));
});

export const login = ({ email, password, code, recaptchaV3, turnstile }) =>
export const login = ({ email, password, code, recaptchaV3, turnstile, captcha }) =>
request({
url: 'token',
method: 'POST',
body: { email, password, code, recaptchaV3, turnstile },
body: { email, password, code, recaptchaV3, turnstile, captcha },
});

export const logout = () => {
Expand Down
Loading