Skip to content

Commit

Permalink
Merge pull request #266 from anqi20/branch-terminate-account
Browse files Browse the repository at this point in the history
Fix reauthenticate error for terminate account function
  • Loading branch information
tsh22 authored Mar 5, 2022
2 parents 22e9b1c + 2ad1c06 commit a3c8e07
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
3 changes: 1 addition & 2 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ export default function App() {
console.log(`User ${data.uid} account terminated!`);
})
.catch((error) => {
console.log(error);
console.log("Error terminating account");
console.log("Error terminating account: ", error);
});
})
.catch((error) => {
Expand Down
48 changes: 40 additions & 8 deletions screens/settings-screens/TerminateAccountScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TouchableWithoutFeedback,
Keyboard,
Image,
Alert,
} from "react-native";
import { Input } from "react-native-elements";
import colors from "../../assets/colors";
Expand All @@ -16,15 +17,37 @@ import firebase from "../../database/firebaseDB";
export default function TerminateAccountScreen({ navigation }) {
const [isMatching, setMatching] = useState(false);
const [showError, setShow] = useState(false);
const [password, setPassword] = useState("");

const user = firebase.auth().currentUser;

// Reauthenticate user, returns a promise
function reauthenticate(email, password) {
var cred = firebase.auth.EmailAuthProvider.credential(email, password);
return user.reauthenticateWithCredential(cred);
}

function onPress() {
if (isMatching) {
setShow(false);
navigation.navigate("Account terminated", { user: user });
if (password != "") {
// If password is entered, attempt reauthentication
reauthenticate(user.email, password)
.then(() => {
console.log("Reauthentication success");
if (isMatching) {
setShow(false);
navigation.navigate("Account terminated", {
user: user,
});
} else {
setShow(true);
}
})
.catch((error) => {
console.log("Unable to reauthenticate");
Alert.alert("Wrong password, try again?");
});
} else {
setShow(true);
Alert.alert("Please enter your password to continue.");
}
}

Expand All @@ -48,15 +71,19 @@ export default function TerminateAccountScreen({ navigation }) {
Are you sure you want to terminate your account?
</Text>
<Input
secureTextEntry={true}
containerStyle={styles.terminateContainer}
placeholder="Password"
onChangeText={(text) => setPassword(text)}
></Input>
<Input
containerStyle={[styles.terminateContainer, { marginBottom: 20 }]}
placeholder="Type 'TERMINATE'"
autoCapitalize="characters"
onChangeText={(text) => validate(text)}
></Input>
{showError ? (
<Text
style={{ color: colors.red, textAlign: "center", marginBottom: 10 }}
>
<Text style={styles.errorText}>
Please try again. Input is case sensitive.
</Text>
) : null}
Expand Down Expand Up @@ -105,12 +132,17 @@ const styles = StyleSheet.create({
color: colors.black,
},
terminateContainer: {
marginVertical: 20,
marginTop: 20,
},
sadSmile: {
alignSelf: "center",
marginBottom: 20,
height: 60,
width: 60,
},
errorText: {
color: colors.red,
textAlign: "center",
marginBottom: 10,
},
});
1 change: 1 addition & 0 deletions screens/settings-screens/TerminateConfirmationScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function TerminateConfirmationScreen({ navigation, route }) {

function terminate() {
return terminateAccount({ user, uid });
// return null;
}

return (
Expand Down

0 comments on commit a3c8e07

Please sign in to comment.