1 |
rakinar2 |
577 |
import axios from "axios"; |
2 |
|
|
import { z } from "zod"; |
3 |
|
|
import { Action } from "../../decorators/Action"; |
4 |
|
|
import { Validate } from "../../decorators/Validate"; |
5 |
|
|
import { logError } from "../../utils/logger"; |
6 |
|
|
import Controller from "../Controller"; |
7 |
|
|
import Request from "../Request"; |
8 |
|
|
import Response from "../Response"; |
9 |
|
|
|
10 |
|
|
const verifySchema = z.object({ |
11 |
|
|
responseToken: z.string() |
12 |
|
|
}); |
13 |
|
|
|
14 |
|
|
export default class VerificationController extends Controller { |
15 |
|
|
@Action("POST", "/challenge/verify") |
16 |
|
|
@Validate(verifySchema) |
17 |
|
|
async verify(request: Request) { |
18 |
|
|
const { responseToken } = request.parsedBody; |
19 |
|
|
|
20 |
|
|
console.log(request.parsedBody); |
21 |
|
|
|
22 |
|
|
try { |
23 |
|
|
const response = await axios.post( |
24 |
|
|
"https://www.google.com/recaptcha/api/siteverify", |
25 |
|
|
new URLSearchParams({ |
26 |
|
|
secret: process.env.RECAPTCHA_SITE_SECRET!, |
27 |
|
|
response: responseToken |
28 |
|
|
}).toString(), |
29 |
|
|
{ |
30 |
|
|
headers: { |
31 |
|
|
"Content-Type": "application/x-www-form-urlencoded" |
32 |
|
|
} |
33 |
|
|
} |
34 |
|
|
); |
35 |
|
|
|
36 |
|
|
console.log(response.data); |
37 |
|
|
|
38 |
|
|
if (response.data.success) { |
39 |
|
|
return { |
40 |
|
|
success: true |
41 |
|
|
}; |
42 |
|
|
} else { |
43 |
|
|
return new Response({ |
44 |
|
|
status: 401, |
45 |
|
|
body: { |
46 |
|
|
success: false, |
47 |
|
|
error: "We were unable to verify you." |
48 |
|
|
} |
49 |
|
|
}); |
50 |
|
|
} |
51 |
|
|
} catch (error) { |
52 |
|
|
logError(error); |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
} |