|
| 1 | +const { tap } = require('./utils'); |
| 2 | + |
| 3 | +module.exports = (AV) => { |
| 4 | + /** |
| 5 | + * @class |
| 6 | + * @example |
| 7 | + * AV.Captcha.request().then(captcha => { |
| 8 | + * captcha.bind({ |
| 9 | + * textInput: 'code', // the id for textInput |
| 10 | + * image: 'captcha', |
| 11 | + * verifyButton: 'verify', |
| 12 | + * }, { |
| 13 | + * success: (validateCode) => {}, // next step |
| 14 | + * error: (error) => {}, // present error.message to user |
| 15 | + * }); |
| 16 | + * }); |
| 17 | + */ |
| 18 | + AV.Captcha = function Captcha(options, authOptions) { |
| 19 | + this._options = options; |
| 20 | + this._authOptions = authOptions; |
| 21 | + /** |
| 22 | + * The image url of the captcha |
| 23 | + * @type string |
| 24 | + */ |
| 25 | + this.url = undefined; |
| 26 | + /** |
| 27 | + * The captchaToken of the captcha. |
| 28 | + * @type string |
| 29 | + */ |
| 30 | + this.captchaToken = undefined; |
| 31 | + /** |
| 32 | + * The validateToken of the captcha. |
| 33 | + * @type string |
| 34 | + */ |
| 35 | + this.validateToken = undefined; |
| 36 | + }; |
| 37 | + |
| 38 | + /** |
| 39 | + * Refresh the captcha |
| 40 | + * @return {Promise.<string>} a new capcha url |
| 41 | + */ |
| 42 | + AV.Captcha.prototype.refresh = function refresh() { |
| 43 | + return AV.Cloud.requestCaptcha(this._options, this._authOptions).then(({ |
| 44 | + captchaToken, url, |
| 45 | + }) => { |
| 46 | + Object.assign(this, { captchaToken, url }); |
| 47 | + return url; |
| 48 | + }); |
| 49 | + }; |
| 50 | + |
| 51 | + /** |
| 52 | + * Verify the captcha |
| 53 | + * @param {String} code The code from user input |
| 54 | + * @return {Promise.<string>} validateToken if the code is valid |
| 55 | + */ |
| 56 | + AV.Captcha.prototype.verify = function verify(code) { |
| 57 | + return AV.Cloud.verifyCaptcha(code, this.captchaToken) |
| 58 | + .then(tap(validateToken => (this.validateToken = validateToken))); |
| 59 | + }; |
| 60 | + |
| 61 | + if (process.env.CLIENT_PLATFORM === 'Browser') { |
| 62 | + /** |
| 63 | + * Bind the captcha to HTMLElements. <b>ONLY AVAILABLE in browsers</b>. |
| 64 | + * @param [elements] |
| 65 | + * @param {String|HTMLInputElement} [elements.textInput] An input element typed text, or the id for the element. |
| 66 | + * @param {String|HTMLImageElement} [elements.image] An image element, or the id for the element. |
| 67 | + * @param {String|HTMLElement} [elements.verifyButton] A button element, or the id for the element. |
| 68 | + * @param [callbacks] |
| 69 | + * @param {Function} [callbacks.success] Success callback will be called if the code is verified. The param `validateCode` can be used for further SMS request. |
| 70 | + * @param {Function} [callbacks.error] Error callback will be called if something goes wrong, detailed in param `error.message`. |
| 71 | + */ |
| 72 | + AV.Captcha.prototype.bind = function bind({ |
| 73 | + textInput, |
| 74 | + image, |
| 75 | + verifyButton, |
| 76 | + }, { |
| 77 | + success, |
| 78 | + error, |
| 79 | + }) { |
| 80 | + if (typeof textInput === 'string') { |
| 81 | + textInput = document.getElementById(textInput); |
| 82 | + if (!textInput) throw new Error(`textInput with id ${textInput} not found`); |
| 83 | + } |
| 84 | + if (typeof image === 'string') { |
| 85 | + image = document.getElementById(image); |
| 86 | + if (!image) throw new Error(`image with id ${image} not found`); |
| 87 | + } |
| 88 | + if (typeof verifyButton === 'string') { |
| 89 | + verifyButton = document.getElementById(verifyButton); |
| 90 | + if (!verifyButton) throw new Error(`verifyButton with id ${verifyButton} not found`); |
| 91 | + } |
| 92 | + |
| 93 | + this.__refresh = () => this.refresh().then(url => { |
| 94 | + image.src = url; |
| 95 | + if (textInput) { |
| 96 | + textInput.value = ''; |
| 97 | + textInput.focus(); |
| 98 | + } |
| 99 | + }).catch(err => console.warn(`refresh captcha fail: ${err.message}`)); |
| 100 | + if (image) { |
| 101 | + this.__image = image; |
| 102 | + image.src = this.url; |
| 103 | + image.addEventListener('click', this.__refresh); |
| 104 | + } |
| 105 | + |
| 106 | + this.__verify = () => { |
| 107 | + const code = textInput.value; |
| 108 | + this.verify(code).catch(err => { |
| 109 | + this.__refresh(); |
| 110 | + throw err; |
| 111 | + }).then(success, error).catch(err => console.warn(`verify captcha fail: ${err.message}`)); |
| 112 | + }; |
| 113 | + if (textInput && verifyButton) { |
| 114 | + this.__verifyButton = verifyButton; |
| 115 | + verifyButton.addEventListener('click', this.__verify); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + /** |
| 120 | + * unbind the captcha from HTMLElements. <b>ONLY AVAILABLE in browsers</b>. |
| 121 | + */ |
| 122 | + AV.Captcha.prototype.unbind = function unbind() { |
| 123 | + if (this.__image) this.__image.removeEventListener('click', this.__refresh); |
| 124 | + if (this.__verifyButton) this.__verifyButton.removeEventListener('click', this.__verify); |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /** |
| 130 | + * Request a captcha |
| 131 | + * @param [options] |
| 132 | + * @param {Number} [options.width] width(px) of the captcha, ranged 60-200 |
| 133 | + * @param {Number} [options.height] height(px) of the captcha, ranged 30-100 |
| 134 | + * @param {Number} [options.size=4] length of the captcha, ranged 3-6. MasterKey required. |
| 135 | + * @param {Number} [options.ttl=60] time to live(s), ranged 10-180. MasterKey required. |
| 136 | + * @return {Promise.<AV.Captcha>} |
| 137 | + */ |
| 138 | + AV.Captcha.request = (options, authOptions) => { |
| 139 | + const captcha = new AV.Captcha(options, authOptions); |
| 140 | + return captcha.refresh().then(() => captcha); |
| 141 | + }; |
| 142 | +}; |
0 commit comments