Skip to content

Commit

Permalink
feat: offline sign in, fix for memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-mccombs committed Jan 26, 2021
1 parent d58884d commit 8fa16c0
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions domains/Auth/SignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const SignIn = ({ navigation }) => {
useEffect(() => {
getData('credentials').then((values) => {
setUser(values);
if (values != null) {
if (values.store === 'Yes') {
setModalVisible(true);
}
});
Expand All @@ -73,8 +73,8 @@ const SignIn = ({ navigation }) => {
Alert.alert(
I18n.t('signIn.unableLogin'),
I18n.t('signIn.usernamePasswordIncorrect'), [
{ text: 'OK' }
],
{ text: 'OK' }
],
{ cancelable: true }
);
};
Expand All @@ -96,11 +96,17 @@ const SignIn = ({ navigation }) => {
{
text: 'Yes',
onPress: () => {
storeData(values, 'credentials');
let credentials = values;
credentials['store'] = 'Yes'
storeData(credentials, 'credentials');
navigation.navigate('StorePincode');
}
},
{ text: 'No', style: 'cancel' },
{
text: 'No', style: 'cancel', onPress: () => {
navigation.navigate('Root')
}
},
],
{ cancelable: false }
// clicking out side of alert will not cancel
Expand All @@ -124,7 +130,13 @@ const SignIn = ({ navigation }) => {
deleteData('credentials');
};

const storeUserInformation = (userData) => {
const storeUserInformation = (userData, userCreds) => {
// store username and password
if (userCreds) {
let credentials = userCreds;
credentials['store'] = 'No'
storeData(credentials, 'credentials');
}
populateCache(userData);
};

Expand All @@ -147,26 +159,28 @@ const SignIn = ({ navigation }) => {
getData('credentials').then((userCreds) => {
// credentials saved do not match those entered, overwrite saved
// credentials
if (userCreds === null || values.username !== userCreds.username
if (userCreds === null || userCreds.store === 'No' || values.username !== userCreds.username
|| values.password !== userCreds.password) {
// Store user organization
storeUserInformation(userData);
storeUserInformation(userData, values);
handleSaveCredentials(values);
} else {
storeUserInformation(userData);
handleSignIn(values, actions.resetForm());
}
}, () => {
// Store user organization
storeUserInformation(userData);
storeUserInformation(userData, values);
// no credentials saved, give option to save
handleSaveCredentials(values);
});
handleSignIn(values, actions.resetForm());
// handleSignIn(values, actions.resetForm());
}, (err) => {
handleFailedAttempt(err);
});
} else {
// offline
console.log("offline")
getData('credentials').then((userCreds) => {
// username and password entered (or saved in creds) match the saved cred
if (values.username === userCreds.username
Expand Down Expand Up @@ -229,8 +243,8 @@ const SignIn = ({ navigation }) => {
{formikProps.isSubmitting ? (
<ActivityIndicator />
) : (
<Button mode="contained" theme={theme} style={styles.submitButton} onPress={formikProps.handleSubmit}>{I18n.t('signIn.login')}</Button>
)}
<Button mode="contained" theme={theme} style={styles.submitButton} onPress={formikProps.handleSubmit}>{I18n.t('signIn.login')}</Button>
)}
<CredentialsModal
modalVisible={modalVisible}
formikProps={formikProps}
Expand Down

0 comments on commit 8fa16c0

Please sign in to comment.