react-native-gigya-sdk
Version:
SAP CDC/Gigya SDK for your React Native applications
78 lines (72 loc) • 3.1 kB
JavaScript
import { NativeModules } from 'react-native';
const {
GigyaSdk
} = NativeModules;
import { GigyaSdkLinkAccountVersions } from '../types';
import getState from './getState';
import sendApiCall from './sendApiCall';
import isGigyaError from './isGigyaError';
import getAccountInfo from './getAccountInfo';
import { setState } from '../internals/state';
import handleSdkCall from '../internals/handleSdkCall';
import clearErrorState from '../internals/clearErrorState';
import saveAuthenticationAttempt from '../internals/saveAuthenticationAttempt';
export default function (loginID, password, options) {
return new Promise(async (resolve, reject) => {
const state = await getState();
const isUsingLinkAccountV1 = state.linkAccountVersion === GigyaSdkLinkAccountVersions.V1;
const resolvePromise = async output => {
try {
await clearErrorState();
} catch (err) {}
resolve(output);
};
try {
const sessionInfo = (options === null || options === void 0 ? void 0 : options.sessionInfo) || state.sessionInfo;
if (isUsingLinkAccountV1) {
var _state$regToken;
const response = await sendApiCall('accounts.linkAccounts', {
loginID,
password,
targetEnv: 'mobile',
...((options === null || options === void 0 ? void 0 : options.noUID) && {
regToken: (options === null || options === void 0 ? void 0 : options.regToken) || ((_state$regToken = state.regToken) === null || _state$regToken === void 0 ? void 0 : _state$regToken.value)
}),
...(!(options !== null && options !== void 0 && options.noUID) && {
UID: (options === null || options === void 0 ? void 0 : options.UID) || state.UID
}),
...((options === null || options === void 0 ? void 0 : options.include) && {
include: options === null || options === void 0 ? void 0 : options.include
})
});
return resolvePromise(response);
} else {
if (!(sessionInfo !== null && sessionInfo !== void 0 && sessionInfo.provider)) throw new Error("no 'sessionInfo.provider' param provided");else if (!(sessionInfo !== null && sessionInfo !== void 0 && sessionInfo.access_token)) throw new Error("no 'sessionInfo.access_token' param provided");
const existingAccount = await handleSdkCall(GigyaSdk.login(loginID, password, '{}'));
await setState({
UID: existingAccount.UID
});
await sendApiCall('accounts.notifySocialLogin', {
targetEnv: 'mobile',
loginMode: 'connect',
providerSessions: {
[sessionInfo.provider]: {
authToken: sessionInfo.access_token
}
}
});
return resolvePromise(await getAccountInfo());
}
} catch (e) {
if (!isUsingLinkAccountV1 && (await isGigyaError(e))) {
try {
await saveAuthenticationAttempt('site', e);
} catch (err) {
return reject(err);
}
}
reject(e);
}
});
}
//# sourceMappingURL=linkToSite.js.map