@arc-publishing/sdk-identity
Version:
JS Identity SDK for working with Identity API
286 lines • 15.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initGoogleLogin = exports.initGoogleAuthSDK = exports.initGoogleAuth = exports.getGoogleAuthInstance = exports.loadGoogleAuthSDK = exports.GOOGLE_SDK_URL = void 0;
var tslib_1 = require("tslib");
var googleSignOn_1 = require("./googleSignOn");
var socialSignOnTypes_1 = require("./socialSignOnTypes");
var identity_1 = tslib_1.__importDefault(require("../sdk/identity"));
var googleSignOn_2 = require("./googleSignOn");
exports.GOOGLE_SDK_URL = 'https://apis.google.com/js/platform.js';
var loadGoogleAuthSDK = function (init) {
console.error(googleSignOn_2.deprecationMessage);
return new Promise(function (resolve, reject) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var element, e_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!document.querySelector("[src=\"".concat(exports.GOOGLE_SDK_URL, "\"]"))) return [3, 1];
element = document.createElement('script');
element.onload = function () {
var _this = this;
window.gapi.load('auth2', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var e_2;
var _a, _b, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
identity_1.default._google = tslib_1.__assign(tslib_1.__assign({}, identity_1.default._google), { auth2: (_b = (_a = window.gapi) === null || _a === void 0 ? void 0 : _a.auth2) !== null && _b !== void 0 ? _b : ((0, socialSignOnTypes_1.isLegacyGoogleSetting)(identity_1.default._google) && ((_c = identity_1.default._google) === null || _c === void 0 ? void 0 : _c.auth2)) });
if (!init) return [3, 4];
_d.label = 1;
case 1:
_d.trys.push([1, 3, , 4]);
return [4, init()];
case 2:
_d.sent();
return [3, 4];
case 3:
e_2 = _d.sent();
return [2, reject(e_2)];
case 4: return [2, resolve()];
}
});
}); });
};
element.onerror = function () {
reject('[Arc Identity SDK] - Error loading Google SDK');
};
element.async = true;
element.src = exports.GOOGLE_SDK_URL;
document.head.appendChild(element);
return [3, 7];
case 1:
if (!window.gapi) {
return [2, reject('[Arc Identity SDK] - Error loading Google SDK')];
}
if (!!window.gapi.auth2) return [3, 2];
window.gapi.load('auth2', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var e_3;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!init) return [3, 4];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, init()];
case 2:
_a.sent();
return [3, 4];
case 3:
e_3 = _a.sent();
return [2, reject(e_3)];
case 4: return [2, resolve()];
}
});
}); });
return [3, 7];
case 2:
if (!init) return [3, 6];
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4, init()];
case 4:
_a.sent();
return [3, 6];
case 5:
e_1 = _a.sent();
return [2, reject(e_1)];
case 6: return [2, resolve()];
case 7: return [2];
}
});
}); });
};
exports.loadGoogleAuthSDK = loadGoogleAuthSDK;
var getGoogleAuthInstance = function () {
console.error(googleSignOn_2.deprecationMessage);
return new Promise(function (resolve, reject) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var e_4;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(0, socialSignOnTypes_1.isLegacyGoogleSetting)(identity_1.default._google)) return [3, 6];
if (!identity_1.default._google.authInstance) return [3, 1];
resolve();
return [3, 5];
case 1:
if (!identity_1.default._google.auth2) return [3, 2];
identity_1.default._google.authInstance = identity_1.default._google.auth2.getAuthInstance();
resolve();
return [3, 5];
case 2:
_a.trys.push([2, 4, , 5]);
return [4, (0, exports.initGoogleAuthSDK)(identity_1.default._google.clientId)];
case 3:
_a.sent();
resolve();
return [3, 5];
case 4:
e_4 = _a.sent();
console.error('Error getting Google Auth instance.');
reject(e_4);
return [3, 5];
case 5: return [3, 7];
case 6:
reject('This method should only be used with the legacy Google Sign In. Migrate to the new Sign In With Google');
_a.label = 7;
case 7: return [2];
}
});
}); });
};
exports.getGoogleAuthInstance = getGoogleAuthInstance;
var initGoogleAuth = function (clientId) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var googleAuthInstance, googleAuth;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
return tslib_1.__generator(this, function (_o) {
switch (_o.label) {
case 0:
console.error(googleSignOn_2.deprecationMessage);
identity_1.default._google = tslib_1.__assign(tslib_1.__assign({}, identity_1.default._google), { auth2: (_b = (_a = window.gapi) === null || _a === void 0 ? void 0 : _a.auth2) !== null && _b !== void 0 ? _b : ((0, socialSignOnTypes_1.isLegacyGoogleSetting)(identity_1.default._google) && ((_c = identity_1.default._google) === null || _c === void 0 ? void 0 : _c.auth2)), clientId: clientId !== null && clientId !== void 0 ? clientId : ((0, socialSignOnTypes_1.isLegacyGoogleSetting)(identity_1.default._google) ? (_d = identity_1.default._google) === null || _d === void 0 ? void 0 : _d.clientId : undefined) });
if (!((_e = identity_1.default._google) === null || _e === void 0 ? void 0 : _e.auth2)) {
throw new Error('[Arc Identity SDK] - Error loading Google SDK');
}
if (!(!((_f = identity_1.default._google) === null || _f === void 0 ? void 0 : _f.clientId) &&
(!identity_1.default.configOptions || (identity_1.default.configOptions && !identity_1.default.configOptions.googleClientId)))) return [3, 2];
return [4, identity_1.default.getConfig()];
case 1:
_o.sent();
_o.label = 2;
case 2:
identity_1.default._google.clientId =
(_h = (_g = identity_1.default._google) === null || _g === void 0 ? void 0 : _g.clientId) !== null && _h !== void 0 ? _h : (_l = (_k = (_j = identity_1.default.configOptions) === null || _j === void 0 ? void 0 : _j.googleClientId) === null || _k === void 0 ? void 0 : _k.split(',')) === null || _l === void 0 ? void 0 : _l[0];
if (!identity_1.default._google.clientId) {
throw new Error('[Arc Identity SDK] - Google Client ID not found, please add one in settings or explicitly pass one to the initGoogleLogin method.');
}
else {
try {
googleAuthInstance = (_m = identity_1.default._google.authInstance) !== null && _m !== void 0 ? _m : identity_1.default._google.auth2.getAuthInstance();
if (!googleAuthInstance) {
googleAuth = identity_1.default._google.auth2.init({
client_id: "".concat(identity_1.default._google.clientId.replace('.apps.googleusercontent.com', ''), ".apps.googleusercontent.com")
});
identity_1.default._google.authInstance = googleAuth;
}
else {
identity_1.default._google.authInstance = googleAuthInstance;
}
}
catch (e) {
console.error('Error while initializing Google sdk:');
throw e;
}
}
return [2];
}
});
}); };
exports.initGoogleAuth = initGoogleAuth;
var initGoogleAuthSDK = function (clientId) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var id_1, e_5;
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
console.error(googleSignOn_2.deprecationMessage);
if (!(0, socialSignOnTypes_1.isLegacyGoogleSetting)(identity_1.default._google)) return [3, 5];
if (clientId && clientId !== ((_a = identity_1.default._google) === null || _a === void 0 ? void 0 : _a.clientId)) {
identity_1.default._google.clientId = clientId;
}
id_1 = identity_1.default._google.clientId;
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4, (0, exports.loadGoogleAuthSDK)(function () { return (0, exports.initGoogleAuth)(id_1); })];
case 2:
_b.sent();
return [3, 4];
case 3:
e_5 = _b.sent();
throw e_5;
case 4: return [3, 6];
case 5: throw new Error('This method should only be used with legacy Google Sign In. Discontinue use & migrate to new Sign In With Google');
case 6: return [2];
}
});
}); };
exports.initGoogleAuthSDK = initGoogleAuthSDK;
function initGoogleLogin(clientId, renderOptions, buttonId, customButton) {
var _a, _b;
if (clientId === void 0) { clientId = ''; }
if (renderOptions === void 0) { renderOptions = {
width: 240,
height: 50,
longtitle: true,
theme: 'dark',
scope: ['profile', 'email']
}; }
if (buttonId === void 0) { buttonId = 'google-sign-in-button'; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var successHandler, defaultRenderOptions, options, e_6;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
console.error(googleSignOn_2.deprecationMessage);
_c.label = 1;
case 1:
_c.trys.push([1, 6, , 7]);
if (!(0, socialSignOnTypes_1.isLegacyGoogleSetting)(identity_1.default._google)) return [3, 4];
if (clientId && clientId !== identity_1.default._google.clientId) {
identity_1.default._google.clientId = clientId;
}
if (!(!identity_1.default._google.auth2 || !identity_1.default._google.authInstance)) return [3, 3];
return [4, (0, exports.initGoogleAuthSDK)(identity_1.default._google.clientId)];
case 2:
_c.sent();
_c.label = 3;
case 3:
if (!identity_1.default._google.authInstance) {
throw new Error('[Arc Identity SDK] - Error initializing Google Auth2 SDK');
}
successHandler = function (googleUser) {
(0, googleSignOn_1.googleSignOn)(googleUser).then(function () {
if (renderOptions && typeof renderOptions.onSuccess === 'function') {
renderOptions.onSuccess(googleUser);
}
});
};
if (renderOptions && typeof renderOptions.onSuccess === 'function') {
identity_1.default._google.onSuccess = renderOptions.onSuccess;
if (typeof renderOptions.onFailure === 'function') {
identity_1.default._google.onFailure = renderOptions.onFailure;
}
}
if (customButton) {
identity_1.default._google.useCustomButton = true;
}
if (!customButton && renderOptions) {
identity_1.default._google.authInstance.attachClickHandler(buttonId, {
scope: ((_a = renderOptions === null || renderOptions === void 0 ? void 0 : renderOptions.scope) !== null && _a !== void 0 ? _a : ['profile', 'email']).join(' '),
prompt: 'select_account'
}, successHandler, (renderOptions && renderOptions.onFailure) || console.error);
defaultRenderOptions = {
width: 240,
height: 50,
longtitle: true,
theme: 'dark'
};
options = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, defaultRenderOptions), renderOptions), { scope: ((_b = renderOptions === null || renderOptions === void 0 ? void 0 : renderOptions.scope) !== null && _b !== void 0 ? _b : ['profile', 'email']).join(' ') });
return [2, window.gapi.signin2.render(buttonId, options)];
}
return [3, 5];
case 4: throw new Error('[Arc Identity SDK] - This method will only work with the legacy Google Sign In. Discontinue use of this method.');
case 5: return [3, 7];
case 6:
e_6 = _c.sent();
console.error('Error while initializing Google login:');
throw e_6;
case 7: return [2];
}
});
});
}
exports.initGoogleLogin = initGoogleLogin;
exports.default = initGoogleLogin;
//# sourceMappingURL=initGoogleLogin.js.map