/[sudobot]/branches/2.x/src/api/public-routes/recaptcha.ts
ViewVC logotype

Annotation of /branches/2.x/src/api/public-routes/recaptcha.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2870 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import axios from "axios";
2     import { Request, Response } from "express";
3     import { json } from "stream/consumers";
4     import DiscordClient from "../../client/Client";
5     import UnverifiedMember from "../../models/UnverifiedMember";
6     import auth from "../Auth";
7     import guildAuth from "../GuildAuth";
8     import { Route } from "../Router";
9    
10     export default <Route> {
11     path: '/recaptcha/:guild/:token/:user_id',
12     async get(req: Request, res: Response) {
13     if (!req.params.guild)
14     return res.json({ msg: 'Guild ID is required', code: 100 });
15    
16     if (!req.params.token)
17     return res.json({ msg: 'Recaptcha token is required', code: 101 });
18    
19     if (!req.params.user_id)
20     return res.json({ msg: 'User ID is required', code: 103 });
21    
22     let guild;
23    
24     try {
25     guild = await DiscordClient.client.guilds.fetch(req.params.guild);
26    
27     if (!DiscordClient.client.config.props[guild.id])
28     throw new Error();
29     }
30     catch (e) {
31     console.log(e);
32     res.json({ msg: 'Invalid Guild ID', code: 102 });
33     return;
34     }
35    
36     if (guild) {
37     console.log(process.env.RECAPTCHA_TOKEN);
38    
39     const response = await axios.post(`https://www.google.com/recaptcha/api/siteverify?response=${decodeURIComponent(req.params.token)}&secret=${decodeURIComponent(process.env.RECAPTCHA_TOKEN!)}&remoteip=${req.ip}`);
40    
41     if (response.data.success) {
42     const memberData = await UnverifiedMember.findOne({
43     where: {
44     user_id: req.params.user_id,
45     guild_id: req.params.guild
46     }
47     });
48    
49     if (!memberData) {
50     return await res.json({
51     msg: "No verification required",
52     code: 104
53     });
54     }
55    
56     try {
57     const user = await guild.members.fetch(memberData.get().user_id);
58    
59     if (!user)
60     throw new Error();
61    
62     await DiscordClient.client.verification.success(user, req);
63    
64     return await res.json({
65     google: response.data,
66     guild,
67     member: user
68     });
69     }
70     catch (e) {
71     console.log(e);
72    
73     return await res.json({
74     msg: "No such member",
75     code: 105
76     });
77     }
78     }
79     else {
80     res.json({
81     google: response.data,
82     });
83     }
84     }
85     }
86     };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26