Skip to content

Commit

Permalink
Merge pull request #171 from waldo-vision/ts/fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Huskydog9988 authored Mar 27, 2023
2 parents 449ee82 + e904eaa commit 5893519
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 44 deletions.
15 changes: 1 addition & 14 deletions apps/web/components/TurnstileWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Box } from '@chakra-ui/react';
import { Turnstile } from '@marsidev/react-turnstile';
import { trpc } from '@utils/trpc';

interface WidgetProps {
valid(valid: boolean, tsToken?: string): void;
Expand All @@ -12,22 +11,10 @@ enum CallbackStates {
}

const TurnstileWidget = (props: WidgetProps) => {
const utils = trpc.useContext();
const verifyToken = trpc.util.verifyUser.useMutation({
async onSuccess() {
await utils.util.invalidate();
},
});
const handleCallback = async (token: string, state: CallbackStates) => {
// check for token
if (state == CallbackStates.SUCCESS) {
const result = await verifyToken.mutateAsync({ tsToken: token });
if (result.result) {
props.valid(true, token);
} else {
props.valid(false, token);
}
console.log(result);
props.valid(true, token);
} else if (state == CallbackStates.ERROR) {
props.valid(false);
} else if (state == CallbackStates.EXPIRE) {
Expand Down
48 changes: 23 additions & 25 deletions apps/web/server/trpc/router/gameplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ export const gameplayRouter = router({
)
.output(GameplaySchema)
.mutation(async ({ input, ctx }) => {
// const isPerson = await vUser(input.tsToken);
// if (!isPerson) {
// throw new TRPCError({
// code: 'BAD_REQUEST',
// message:
// 'We could not confirm if you were a legitimate user. Please refresh the page and try again.',
// // not sure if its safe to give this to the user
// cause: '',
// });
// }
const isPerson = await vUser(input.tsToken);
if (!isPerson) {
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We could not confirm if you were a legitimate user. Please refresh the page and try again.',
// not sure if its safe to give this to the user
cause: '',
});
}
const existingGameplay = await ctx.prisma.gameplay.findUnique({
where: {
youtubeUrl: input.youtubeUrl,
Expand All @@ -185,7 +185,8 @@ export const gameplayRouter = router({
}
try {
// Validate that the URL contains a video that can be downloaded.
await ytdl.getInfo(input.youtubeUrl);
// don't need this yet and its just causing useless errors preventing people from submitting.
// await ytdl.getInfo(input.youtubeUrl);
// Download video and save as a local MP4 to be used for processing.
// await ytdl(url).pipe(fs.createWriteStream(`${data.id}.mp4`));

Expand Down Expand Up @@ -449,16 +450,16 @@ export const gameplayRouter = router({
)
.output(ReviewItemsGameplaySchema)
.query(async ({ input, ctx }) => {
// const isPerson = await vUser(input.tsToken);
// if (!isPerson) {
// throw new TRPCError({
// code: 'BAD_REQUEST',
// message:
// 'We could not confirm if you were a legitimate user. Please refresh the page and try again.',
// // not sure if its safe to give this to the user
// cause: '',
// });
// }
const isPerson = await vUser(input.tsToken);
if (!isPerson) {
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We could not confirm if you were a legitimate user. Please refresh the page and try again.',
// not sure if its safe to give this to the user
cause: '',
});
}
const randomPick = (values: string[]) => {
const index = Math.floor(Math.random() * values.length);
return values[index];
Expand Down Expand Up @@ -495,11 +496,8 @@ export const gameplayRouter = router({
)
.output(z.object({ message: z.string() }))
.mutation(async ({ input, ctx }) => {
console.log(input.tsToken);
const isPerson = await vUser(input.tsToken);
console.log('isPerson: ');
console.log(isPerson);
if (isPerson == false) {
if (!isPerson) {
throw new TRPCError({
code: 'BAD_REQUEST',
message:
Expand Down
6 changes: 1 addition & 5 deletions apps/web/server/trpc/router/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { router, protectedProcedure } from '../trpc';
import { serverSanitize } from '@utils/sanitize';

export async function vUser(tsToken: string) {
console.log(tsToken);
const endpoint = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
const body = `secret=${encodeURIComponent(
process.env.CLOUDFLARE_TURNSTILE_SECRET,
Expand All @@ -16,12 +15,9 @@ export async function vUser(tsToken: string) {
},
});
const result = await request.json();

if (result.success == true) {
console.log('true');
if (result.success) {
return true;
} else {
console.log('false');
return false;
}
}
Expand Down

0 comments on commit 5893519

Please sign in to comment.