Skip to content

Commit

Permalink
Add re-chapta to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Jan 22, 2024
1 parent f679429 commit 246b2e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/client/components/DemoForm/DemoForm.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import React, { useContext, useState } from 'react';
import { Alert, Button } from 'react-bootstrap';
import React, { useContext, useRef, useState } from 'react';
import { Alert } from 'react-bootstrap';
import { BsChatSquareTextFill, BsPersonFillSlash, BsStopwatchFill, BsTrashFill } from 'react-icons/bs';
import { useNavigate } from 'react-router-dom';
import { EndpointResponse, UserLoginResponse } from '../../../types/endpoints';
import { UserContext } from '../../context/UserContext';
import getAxios from '../../helpers/axios';
import InteractiveButton from '../InteractiveButton/InteractiveButton';
import { BsChatSquareTextFill, BsPersonFillSlash, BsStopwatchFill, BsTrashFill } from 'react-icons/bs';
import DokChatCaptcha from '../DokChatCaptcha/DokChatCaptcha';
import ReCAPTCHA from 'react-google-recaptcha';

const axios = getAxios();

function DemoForm() {
const [ user, updateToken, setUser ] = useContext(UserContext);
const [ loading, setLoading ] = useState(false);
const [ error, setError ] = useState<string | null>(null);
const captchaRef = useRef<ReCAPTCHA>(null!);
const navigate = useNavigate();

return (
Expand Down Expand Up @@ -65,13 +68,20 @@ function DemoForm() {
Create demo account
</InteractiveButton>
</div>

<DokChatCaptcha ref={captchaRef} />
</div>
);

async function useDemoAccount(event: React.FormEvent) {
event.preventDefault();
setLoading(true);
await axios.post('/auth/demo')

// Allow for empty response, if server don't send site keys
// It means reCAPTCHA is disabled
const reCaptchaResponse = captchaRef.current ? await captchaRef.current.executeAsync() : null;

await axios.post('/auth/demo', { reCaptchaResponse })
.then((r: any) => {
const resp: EndpointResponse<UserLoginResponse> = r.data;
setTimeout(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/server/routes/auth/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import AuthManager from '../../managers/authManager';
import UserManager from '../../managers/userManager';
import allowedMethods from '../../middlewares/allowedMethods';
import jwtManager from '../../managers/jwtManager';
import ensureCaptcha from '../../middlewares/ensureCaptcha';

const router = express.Router();

router.all('/demo',
allowedMethods('POST'),
ensureCaptcha(),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return new ApiResponse(res).validationError(errors);
Expand Down

0 comments on commit 246b2e0

Please sign in to comment.