Skip to content

Commit 5f1be1d

Browse files
committed
Created login/signup flow with error catching and showing the error to the user
1 parent 8ef563d commit 5f1be1d

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/components/LoginForm.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import React, { Component } from "react";
2+
import { Text } from "react-native";
3+
24
import firebase from "firebase";
5+
36
import { Button, Card, CardSection, Input } from "./common";
47

58
class LoginForm extends Component {
69
constructor(props) {
710
super(props);
811
this.state = {
912
email: "",
10-
password: ""
13+
password: "",
14+
error: ""
1115
};
1216
}
1317

14-
onButtonPress = () => {
18+
onButtonPress = async () => {
1519
const { email, password } = this.state;
16-
firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password);
20+
try {
21+
await firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password);
22+
} catch (err) {
23+
try {
24+
await firebase.auth().createUserWithEmailAndPassword(email, password);
25+
} catch (error) {
26+
this.setState({ error: "Authentication Failed" });
27+
}
28+
}
1729
};
1830

1931
render() {
@@ -36,6 +48,9 @@ class LoginForm extends Component {
3648
secureTextEntry
3749
/>
3850
</CardSection>
51+
52+
<Text style={styles.errorTextStyle}>{this.state.error}</Text>
53+
3954
<CardSection>
4055
<Button onPress={this.onButtonPress}>Login</Button>
4156
</CardSection>
@@ -44,4 +59,12 @@ class LoginForm extends Component {
4459
}
4560
}
4661

62+
const styles = {
63+
errorTextStyle: {
64+
fontSize: 20,
65+
alignSelf: "center",
66+
color: "red"
67+
}
68+
};
69+
4770
export default LoginForm;

0 commit comments

Comments
 (0)