auth0-lock
Version:
Auth0 Lock
174 lines (163 loc) • 6.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getVersion = getVersion;
exports.loginCallback = loginCallback;
exports.normalizeAuthParams = normalizeAuthParams;
exports.normalizeError = normalizeError;
exports.trimAuthParams = trimAuthParams;
exports.webAuthOverrides = webAuthOverrides;
var _excluded = ["popup"];
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); }
function _objectDestructuringEmpty(t) { if (null == t) throw new TypeError("Cannot destructure " + t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function normalizeError(error, domain) {
if (!error) {
return error;
}
// TODO: clean this mess, the first checks are for social/popup,
// then we have some stuff for passwordless and the latter is for
// db.
// TODO: the following checks were copied from https://github.com/auth0/lock/blob/0a5abf1957c9bb746b0710b274d0feed9b399958/index.js#L1263-L1288
// Some of the checks are missing because I couldn't reproduce them and I'm
// afraid they'll break existent functionality if add them.
// We need a better error handling story in auth0.js.
if (error.status === 'User closed the popup window') {
// {
// status: "User closed the popup window",
// name: undefined,
// code: undefined,
// details: {
// description: "server error",
// code: undefined
// }
// }
return {
code: 'lock.popup_closed',
error: 'lock.popup_closed',
description: 'Popup window closed.'
};
}
if (error.code === 'unauthorized') {
// Custom rule error
//
// {
// "code": "unauthorized",
// "details": {
// "code": "unauthorized",
// "error_description": "user is blocked",
// "error": "unauthorized"
// },
// "name": "unauthorized",
// "status": 401
// }
// Default "user is blocked" rule error
//
// {
// "code": "unauthorized",
// "details": {
// "code": "unauthorized",
// "error_description": "user is blocked",
// "error": "unauthorized"
// },
// "name": "unauthorized",
// "status": 401
// }
// Social cancel permissions.
//
// {
// code: "unauthorized",
// details: {
// code: "unauthorized"
// error: "unauthorized"
// error_description: "access_denied"
// },
// name: "unauthorized"
// status: 401
// }
// Social cancel permissions or unknown error
if (!error.description || error.description === 'access_denied') {
return {
code: 'lock.unauthorized',
error: 'lock.unauthorized',
description: error.description || 'Permissions were not granted.'
};
}
// Special case for custom rule error
if (error.description === 'user is blocked') {
return {
code: 'blocked_user',
error: 'blocked_user',
description: error.description
};
}
// Custom Rule error
return {
code: 'rule_error',
error: 'rule_error',
description: error.description
};
}
if (window.location.host !== domain && (error.error === 'access_denied' || error.code === 'access_denied')) {
return {
code: 'invalid_user_password',
error: 'invalid_user_password',
description: error.description
};
}
if (error.code === 'invalid_captcha') {
return _defineProperty(_defineProperty({
code: 'invalid_captcha'
}, "code", 'invalid_captcha'), "description", error.description);
}
var result = {
error: error.code ? error.code : error.statusCode || error.error,
description: error.description || error.code
};
// result is used for passwordless and error for database.
return result.error === undefined && result.description === undefined ? error : result;
}
function loginCallback(redirect, domain, cb) {
return redirect ? function (error) {
return cb(normalizeError(error, domain));
} : function (error, result) {
return cb(normalizeError(error, domain), result);
};
}
function normalizeAuthParams(_ref2) {
var popup = _ref2.popup,
authParams = _objectWithoutProperties(_ref2, _excluded);
return authParams;
}
function webAuthOverrides() {
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
__tenant = _ref3.__tenant,
__token_issuer = _ref3.__token_issuer,
__jwks_uri = _ref3.__jwks_uri;
if (__tenant || __token_issuer || __jwks_uri) {
return {
__tenant: __tenant,
__token_issuer: __token_issuer,
__jwks_uri: __jwks_uri
};
}
return null;
}
function trimAuthParams() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var p = Object.assign({}, (_objectDestructuringEmpty(params), params));
['username', 'email', 'phoneNumber', 'mfa_code'].forEach(function (k) {
if (typeof p[k] === 'string') {
p[k] = p[k].trim();
}
});
return p;
}
function getVersion() {
return "14.2.5";
}