auth0-lock
Version:
Auth0 Lock
136 lines (130 loc) • 5.6 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Flow = void 0;
exports.getCaptchaConfig = getCaptchaConfig;
exports.setCaptchaParams = setCaptchaParams;
exports.showMissingCaptcha = showMissingCaptcha;
exports.swapCaptcha = swapCaptcha;
var l = _interopRequireWildcard(require("../core/index"));
var c = _interopRequireWildcard(require("../field/index"));
var i18n = _interopRequireWildcard(require("../i18n"));
var _index3 = require("../store/index");
var _web_api = _interopRequireDefault(require("../core/web_api"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var Flow = exports.Flow = Object.freeze({
DEFAULT: 'default',
SIGNUP: 'signup',
PASSWORDLESS: 'passwordless',
PASSWORD_RESET: 'password_reset'
});
/**
* Return the captcha config object based on the type of flow.
*
* @param {Object} m model
* @param {Flow} flow Which flow the captcha is being rendered in
*/
function getCaptchaConfig(m, flow) {
if (flow === Flow.PASSWORD_RESET) {
return l.passwordResetCaptcha(m);
} else if (flow === Flow.PASSWORDLESS) {
return l.passwordlessCaptcha(m);
} else if (flow === Flow.SIGNUP) {
return l.signupCaptcha(m);
} else {
return l.captcha(m);
}
}
/**
* Display the error message of missing captcha in the header of lock.
*
* @param {Object} m model
* @param {Number} id
* @param {Flow} flow Which flow the captcha is being rendered in
*/
function showMissingCaptcha(m, id) {
var flow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Flow.DEFAULT;
var captchaConfig = getCaptchaConfig(m, flow);
var captchaError = captchaConfig.get('provider') === 'recaptcha_v2' || captchaConfig.get('provider') === 'recaptcha_enterprise' || captchaConfig.get('provider') === 'hcaptcha' || captchaConfig.get('provider') === 'auth0_v2' || captchaConfig.get('provider') === 'friendly_captcha' || captchaConfig.get('provider') === 'arkose' ? 'invalid_recaptcha' : 'invalid_captcha';
var errorMessage = i18n.html(m, ['error', 'login', captchaError]);
(0, _index3.swap)(_index3.updateEntity, 'lock', id, function (m) {
m = l.setSubmitting(m, false, errorMessage);
return c.showInvalidField(m, 'captcha');
});
return m;
}
/**
* Set the captcha value in the fields object before sending the request.
*
* @param {Object} m model
* @param {Object} params
* @param {Flow} flow Which flow the captcha is being rendered in
* @param {Object} fields
*
* @returns {Boolean} returns true if is required and missing the response from the user
*/
function setCaptchaParams(m, params, flow, fields) {
var captchaConfig = getCaptchaConfig(m, flow);
var isCaptchaRequired = captchaConfig && captchaConfig.get('required');
if (!isCaptchaRequired) {
return true;
}
var captcha = c.getFieldValue(m, 'captcha');
// captcha required and missing
if (!captcha) {
return false;
}
params['captcha'] = captcha;
fields.push('captcha');
return true;
}
/**
* Get a new challenge and display the new captcha image.
*
* @param {number} id The id of the Lock instance.
* @param {Flow} flow Which flow the captcha is being rendered in.
* @param {boolean} wasInvalid A boolean indicating if the previous captcha was invalid.
* @param {Function} [next] A callback.
*/
function swapCaptcha(id, flow, wasInvalid, next) {
if (flow === Flow.PASSWORD_RESET) {
return _web_api.default.getPasswordResetChallenge(id, function (err, newCaptcha) {
if (!err && newCaptcha) {
(0, _index3.swap)(_index3.updateEntity, 'lock', id, l.setPasswordResetCaptcha, newCaptcha, wasInvalid);
}
if (next) {
next();
}
});
} else if (flow === Flow.PASSWORDLESS) {
return _web_api.default.getPasswordlessChallenge(id, function (err, newCaptcha) {
if (!err && newCaptcha) {
(0, _index3.swap)(_index3.updateEntity, 'lock', id, l.setPasswordlessCaptcha, newCaptcha, wasInvalid);
}
if (next) {
next();
}
});
} else if (flow === Flow.SIGNUP) {
return _web_api.default.getSignupChallenge(id, function (err, newCaptcha) {
if (!err && newCaptcha) {
(0, _index3.swap)(_index3.updateEntity, 'lock', id, l.setSignupChallenge, newCaptcha, wasInvalid);
}
if (next) {
next();
}
});
} else {
return _web_api.default.getChallenge(id, function (err, newCaptcha) {
if (!err && newCaptcha) {
(0, _index3.swap)(_index3.updateEntity, 'lock', id, l.setCaptcha, newCaptcha, wasInvalid);
}
if (next) {
next();
}
});
}
}