@coolgk/utils
Version:
javascript, typescript utility and wrapper functions and classes: array, string, base64, ampq, bcrypt, cache, captcha, csv, email, jwt, number, pdf, tmp, token, unit conversion, url params, session, form data, google sign in, facebook sign in
67 lines (65 loc) • 1.85 kB
JavaScript
/*!
* @package @coolgk/utils
* @version 3.1.4
* @link https://github.com/coolgk/node-utils
* @license MIT
* @author Daniel Gong <daniel.k.gong@gmail.com>
*
* Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved.
* Licensed under the MIT License.
*/
;
/*!
* Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved.
* Licensed under the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const request = require("request");
class Captcha {
constructor(options) {
this._request = options.request || request;
this._secret = options.secret;
}
verify(response, remoteip) {
return new Promise((resolve, reject) => {
this._request.post({
url: Captcha._RECAPTCHA_URL,
form: {
secret: this._secret,
response,
remoteip
}
}, (error, httpResponse, data) => {
if (error) {
reject(error);
}
else {
resolve(JSON.parse(data));
}
});
});
}
}
Captcha._RECAPTCHA_URL = 'https://www.google.com/recaptcha/api/siteverify';
exports.Captcha = Captcha;
function verify(secret, response, remoteip) {
return new Promise((resolve, reject) => {
request.post({
url: Captcha._RECAPTCHA_URL,
form: {
secret,
response,
remoteip
}
}, (error, httpResponse, data) => {
if (error) {
reject(error);
}
else {
resolve(JSON.parse(data));
}
});
});
}
exports.verify = verify;
exports.default = Captcha;