@passageidentity/passage-react-native
Version:
Passkey Complete for React Native - Go completely passwordless with a standalone auth solution in your React Native app with Passage by 1Password
62 lines (60 loc) • 2.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.waitForDeepLinkQueryValues = void 0;
var _reactNative = require("react-native");
const getUrlParamValue = (url, param) => {
const queryString = url.split('?')[1];
const queryParams = queryString ? queryString.split('&').reduce((acc, current) => {
const [key, value] = current.split('=');
if (key && value) {
acc[key] = decodeURIComponent(value);
}
return acc;
}, {}) : {};
return queryParams[param] || null;
};
/**
* Call this function when expecting a redirect into the application when the app has
* become inactive or backgrounded.
*
* Note that this function works best in a "singleTask" Android app.
*
* @param parameters Array of parameters to return from Deep Link.
* @returns object
* @throws
*/
const waitForDeepLinkQueryValues = async parameters => {
return new Promise(async (resolve, reject) => {
// We need to listen for a Deep Link url event, and attempt to extract the parameters
// from the redirect url.
const linkingListener = _reactNative.Linking.addEventListener('url', async event => {
appStateListener.remove();
linkingListener.remove();
const {
url
} = event;
const result = {};
for (let parameter of parameters) {
const value = getUrlParamValue(url, parameter);
if (!value) {
return reject(new Error(`Missing query parameter ${parameter} in redirect url.`));
}
result[parameter] = value;
}
resolve(result);
});
// We need to listen for an AppState change event in the case that the user returns to
// the app without having been redirected.
// Note that appStateListener will NEVER be triggered before linkingListener.
const appStateListener = _reactNative.AppState.addEventListener('change', nextAppState => {
if (nextAppState !== 'active') return;
appStateListener.remove();
linkingListener.remove();
reject(new Error('Operation canceled.'));
});
});
};
exports.waitForDeepLinkQueryValues = waitForDeepLinkQueryValues;
//# sourceMappingURL=waitForDeepLinkQueryValues.js.map
;