UNPKG

@arc-publishing/sdk-identity

Version:
278 lines 14.4 kB
import { __assign, __awaiter, __generator } from "tslib"; import { googleSignOn } from './googleSignOn'; import { isLegacyGoogleSetting } from './socialSignOnTypes'; import Identity from '../sdk/identity'; import { deprecationMessage } from './googleSignOn'; export var GOOGLE_SDK_URL = 'https://apis.google.com/js/platform.js'; export var loadGoogleAuthSDK = function (init) { console.error(deprecationMessage); return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () { var element, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!!document.querySelector("[src=\"".concat(GOOGLE_SDK_URL, "\"]"))) return [3, 1]; element = document.createElement('script'); element.onload = function () { var _this = this; window.gapi.load('auth2', function () { return __awaiter(_this, void 0, void 0, function () { var e_2; var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: Identity._google = __assign(__assign({}, Identity._google), { auth2: (_b = (_a = window.gapi) === null || _a === void 0 ? void 0 : _a.auth2) !== null && _b !== void 0 ? _b : (isLegacyGoogleSetting(Identity._google) && ((_c = Identity._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 = 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 __awaiter(void 0, void 0, void 0, function () { var e_3; return __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]; } }); }); }); }; export var getGoogleAuthInstance = function () { console.error(deprecationMessage); return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () { var e_4; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!isLegacyGoogleSetting(Identity._google)) return [3, 6]; if (!Identity._google.authInstance) return [3, 1]; resolve(); return [3, 5]; case 1: if (!Identity._google.auth2) return [3, 2]; Identity._google.authInstance = Identity._google.auth2.getAuthInstance(); resolve(); return [3, 5]; case 2: _a.trys.push([2, 4, , 5]); return [4, initGoogleAuthSDK(Identity._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]; } }); }); }); }; export var initGoogleAuth = function (clientId) { return __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 __generator(this, function (_o) { switch (_o.label) { case 0: console.error(deprecationMessage); Identity._google = __assign(__assign({}, Identity._google), { auth2: (_b = (_a = window.gapi) === null || _a === void 0 ? void 0 : _a.auth2) !== null && _b !== void 0 ? _b : (isLegacyGoogleSetting(Identity._google) && ((_c = Identity._google) === null || _c === void 0 ? void 0 : _c.auth2)), clientId: clientId !== null && clientId !== void 0 ? clientId : (isLegacyGoogleSetting(Identity._google) ? (_d = Identity._google) === null || _d === void 0 ? void 0 : _d.clientId : undefined) }); if (!((_e = Identity._google) === null || _e === void 0 ? void 0 : _e.auth2)) { throw new Error('[Arc Identity SDK] - Error loading Google SDK'); } if (!(!((_f = Identity._google) === null || _f === void 0 ? void 0 : _f.clientId) && (!Identity.configOptions || (Identity.configOptions && !Identity.configOptions.googleClientId)))) return [3, 2]; return [4, Identity.getConfig()]; case 1: _o.sent(); _o.label = 2; case 2: Identity._google.clientId = (_h = (_g = Identity._google) === null || _g === void 0 ? void 0 : _g.clientId) !== null && _h !== void 0 ? _h : (_l = (_k = (_j = Identity.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._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._google.authInstance) !== null && _m !== void 0 ? _m : Identity._google.auth2.getAuthInstance(); if (!googleAuthInstance) { googleAuth = Identity._google.auth2.init({ client_id: "".concat(Identity._google.clientId.replace('.apps.googleusercontent.com', ''), ".apps.googleusercontent.com") }); Identity._google.authInstance = googleAuth; } else { Identity._google.authInstance = googleAuthInstance; } } catch (e) { console.error('Error while initializing Google sdk:'); throw e; } } return [2]; } }); }); }; export var initGoogleAuthSDK = function (clientId) { return __awaiter(void 0, void 0, void 0, function () { var id_1, e_5; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: console.error(deprecationMessage); if (!isLegacyGoogleSetting(Identity._google)) return [3, 5]; if (clientId && clientId !== ((_a = Identity._google) === null || _a === void 0 ? void 0 : _a.clientId)) { Identity._google.clientId = clientId; } id_1 = Identity._google.clientId; _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); return [4, loadGoogleAuthSDK(function () { return 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]; } }); }); }; export 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 __awaiter(this, void 0, void 0, function () { var successHandler, defaultRenderOptions, options, e_6; return __generator(this, function (_c) { switch (_c.label) { case 0: console.error(deprecationMessage); _c.label = 1; case 1: _c.trys.push([1, 6, , 7]); if (!isLegacyGoogleSetting(Identity._google)) return [3, 4]; if (clientId && clientId !== Identity._google.clientId) { Identity._google.clientId = clientId; } if (!(!Identity._google.auth2 || !Identity._google.authInstance)) return [3, 3]; return [4, initGoogleAuthSDK(Identity._google.clientId)]; case 2: _c.sent(); _c.label = 3; case 3: if (!Identity._google.authInstance) { throw new Error('[Arc Identity SDK] - Error initializing Google Auth2 SDK'); } successHandler = function (googleUser) { googleSignOn(googleUser).then(function () { if (renderOptions && typeof renderOptions.onSuccess === 'function') { renderOptions.onSuccess(googleUser); } }); }; if (renderOptions && typeof renderOptions.onSuccess === 'function') { Identity._google.onSuccess = renderOptions.onSuccess; if (typeof renderOptions.onFailure === 'function') { Identity._google.onFailure = renderOptions.onFailure; } } if (customButton) { Identity._google.useCustomButton = true; } if (!customButton && renderOptions) { Identity._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 = __assign(__assign(__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]; } }); }); } export default initGoogleLogin; //# sourceMappingURL=initGoogleLogin.js.map