Skip to content

Commit

Permalink
chore: adapt new api
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed May 15, 2024
1 parent 77722be commit 8d35fb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
12 changes: 3 additions & 9 deletions packages/backend/server/src/plugins/captcha/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common';

import { Public } from '../../core/auth';
import { AuthThrottlerGuard, Throttle } from '../../fundamentals';
import { Throttle } from '../../fundamentals';
import { CaptchaService } from './service';

@Throttle('strict')
@Controller('/api/auth')
export class CaptchaController {
constructor(private readonly captcha: CaptchaService) {}

@Public()
@UseGuards(AuthThrottlerGuard)
@Throttle({
default: {
limit: 60,
ttl: 60,
},
})
@Get('/challenge')
async getChallenge() {
return this.captcha.getChallengeToken();
Expand Down
13 changes: 10 additions & 3 deletions packages/frontend/core/src/components/affine/auth/use-captcha.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { apis } from '@affine/electron-api';
import { Turnstile } from '@marsidev/react-turnstile';
import { useLiveData, useService } from '@toeverything/infra';
import { atom, useAtom, useSetAtom } from 'jotai';
import { useEffect, useRef } from 'react';
import useSWR from 'swr';

import { useServerFeatures } from '../../../hooks/affine/use-server-config';
import { ServerConfigService } from '../../../modules/cloud';
import * as style from './style.css';

type Challenge = {
Expand Down Expand Up @@ -39,10 +40,16 @@ const generateChallengeResponse = async (challenge: string) => {
const captchaAtom = atom<string | undefined>(undefined);
const responseAtom = atom<string | undefined>(undefined);

const useHasCaptcha = () => {
const serverConfig = useService(ServerConfigService).serverConfig;
const hasCaptcha = useLiveData(serverConfig.features$.map(r => r?.captcha));
return hasCaptcha || false;
};

export const Captcha = () => {
const setCaptcha = useSetAtom(captchaAtom);
const [response] = useAtom(responseAtom);
const { captcha: hasCaptchaFeature } = useServerFeatures();
const hasCaptchaFeature = useHasCaptcha();

if (!hasCaptchaFeature) {
return null;
Expand All @@ -68,7 +75,7 @@ export const Captcha = () => {
export const useCaptcha = (): [string | undefined, string?] => {
const [verifyToken] = useAtom(captchaAtom);
const [response, setResponse] = useAtom(responseAtom);
const { captcha: hasCaptchaFeature } = useServerFeatures();
const hasCaptchaFeature = useHasCaptcha();

const { data: challenge } = useSWR('/api/auth/challenge', challengeFetcher, {
suspense: false,
Expand Down

0 comments on commit 8d35fb8

Please sign in to comment.