Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
AsyncStorage.getItem('firstLaunchTime').then(startTime => {
if (startTime !== null && !isNaN(parseInt(startTime, 10))) {
// We don't need this value anymore once we've retrieved it
AsyncStorage.removeItem('firstLaunchTime');
// We know this is the first app launch because firstLaunchTime is set and it's a valid number
const start = parseInt(startTime, 10);
const now = moment().unix();
const delta = now - start;
AsyncStorage.getItem('firstLaunchSuspended').then(suspended => {
AsyncStorage.removeItem('firstLaunchSuspended');
const appSuspended = suspended === 'true';
if (NativeModules.Firebase) {
NativeModules.Firebase.track('first_run_time', {
total_seconds: delta,
app_suspended: appSuspended,
});
}
});
}
});
const loadNavigationState = async () => {
try {
const navState = JSON.parse(await AsyncStorage.getItem(NAV_STORE_KEY));
const didCrashLastOpen = JSON.parse(
await AsyncStorage.getItem(ERROR_STORE_KEY)
);
// Clear error saved state so that navigation persistence happens on next load
await AsyncStorage.setItem(ERROR_STORE_KEY, JSON.stringify(false));
// If the app crashed last time, don't restore nav state
log("DID CRASH?", didCrashLastOpen);
return didCrashLastOpen ? null : navState;
} catch (err) {
log("Error reading navigation and error state", err);
}
};
async function getItemFromStorage(key) {
try {
return await AsyncStorage.getItem(key);
} catch (error) {
console.error(`Error retrieving item with key: ${key}`);
console.error(error.message);
}
return null;
}
async function register(navigation:any):Promise {
console.log('[TransistorAuth] this device requires reqistration');
await BackgroundGeolocation.destroyTransistorAuthorizationToken(ENV.TRACKER_HOST);
let orgname = await AsyncStorage.getItem("orgname");
let username = await AsyncStorage.getItem("username");
if (orgname == null || username == null) {
Util.navigateHome(navigation);
return {
accessToken: "DUMMY_TOKEN",
refreshToken: "DUMMY_TOKEN",
expires: -1,
url: ''
};
}
let token:TransistorAuthorizationToken = await BackgroundGeolocation.findOrCreateTransistorAuthorizationToken(orgname, username, ENV.TRACKER_HOST);
await BackgroundGeolocation.setConfig({
transistorAuthorizationToken: token
});
componentWillMount(): void {
AsyncStorage.getItem('devKeepPage')
.then(devKeepPage => {
this.setState({
devKeepPage: !!devKeepPage
});
})
.catch(e => console.log('cannot get devKeepPage flag from AsyncStorage', e));
}
requestPushNotificationsPermission = async () => {
const promptCount = await AsyncStorage.getItem('@MetaMask:pushNotificationsPromptCount');
if (!promptCount || Number(promptCount) < AppConstants.MAX_PUSH_NOTIFICATION_PROMPT_TIMES) {
PushNotification.checkPermissions(permissions => {
if (!permissions || !permissions.alert) {
Alert.alert(
strings('notifications.prompt_title'),
strings('notifications.prompt_desc'),
[
{
text: strings('notifications.prompt_cancel'),
onPress: () => false,
style: 'default'
},
{
text: strings('notifications.prompt_ok'),
onPress: () => PushNotification.requestPermissions()
}
async function handleLogin() {
setLoading(true);
const response = await handleLoginFacebook();
if (response.error) {
throwAuthenticationError();
}
if (response.user) {
const { accessToken } = JSON.parse(await AsyncStorage.getItem('@facebook:accessData'));
await AsyncStorage.setItem('@animavita:facebook_user', JSON.stringify(response.user));
loginUser({
variables: { accessToken },
});
}
}
return new Promise(resolve => {
if (this.clientId) {
resolve(this.clientId);
} else {
AsyncStorage.getItem("@ClientId").then(clientId => {
if (clientId && clientId.length > 0) {
this.clientId = clientId;
resolve(clientId);
} else {
this.clientId = randomBytes(32);
AsyncStorage.setItem("@ClientId", this.clientId);
resolve(this.clientId);
}
});
}
});
}
async getAuthorization() {
let token = await AsyncStorage.getItem(Constant.TOKEN_KEY);
if (!token) {
let basic = await AsyncStorage.getItem(Constant.USER_BASIC_CODE);
if (!basic) {
//提示输入账号密码
} else {
//通过 basic 去获取token,获取到设置,返回token
return `Basic ${basic}`;
}
} else {
this.optionParams.authorizationCode = token;
return token;
}
}