Skip to content

Commit

Permalink
Merge pull request #75 from ava-labs/fix/v2-recaptcha-2
Browse files Browse the repository at this point in the history
fix: v2 recaptcha
  • Loading branch information
rajranjan0608 authored Nov 21, 2022
2 parents 4757ea7 + 06291e1 commit 0a90118
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
30 changes: 21 additions & 9 deletions middlewares/verifyCaptcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const axios = require('axios')

export class VerifyCaptcha {
secret: string
v2secret?: string
v2secret: string
middleware = (req: any, res: any, next: () => void) => this.verifyCaptcha(req, res, next)

constructor(app: any, CAPTCHA_SECRET: string, V2_CAPTCHA_SECRET?: string) {
constructor(app: any, CAPTCHA_SECRET: string, V2_CAPTCHA_SECRET: string) {
if(typeof CAPTCHA_SECRET != "string") {
throw "Captcha Secret should be string"
}
Expand All @@ -15,19 +15,31 @@ export class VerifyCaptcha {

async verifyV2Token(v2Token: string): Promise<boolean> {
if(v2Token) {
const URL = `https://www.google.com/recaptcha/api/siteverify?secret=${this.v2secret}&response=${v2Token}`
const URL = "https://www.google.com/recaptcha/api/siteverify"
let response


let recaptchaBody = {
secret: `${this.v2secret}`,
response: `${v2Token}`,
}

try {
response = await axios.post(URL)
} catch(err: any){
console.log("Recaptcha V2 error:", err?.message)
response = await axios
.get(URL, {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
params: recaptchaBody,
})
.then((r: any) => {
return r
})
} catch (err: any) {
console.log("Recaptcha V2 error:", err?.message)
}

const data = response?.data

if(data?.success) {
return true;
return true
}
}

Expand Down
2 changes: 1 addition & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ new RateLimiter(app, [
}
})

const captcha: VerifyCaptcha = new VerifyCaptcha(app, process.env.CAPTCHA_SECRET!, process.env.V2_CAPTCHA_SECRET)
const captcha: VerifyCaptcha = new VerifyCaptcha(app, process.env.CAPTCHA_SECRET!, process.env.V2_CAPTCHA_SECRET!)

let evms = new Map<string, EVMInstanceAndConfig>()

Expand Down

0 comments on commit 0a90118

Please sign in to comment.