Skip to content

Commit f9d0df5

Browse files
committed
Add behaviour to show user a logout button if it's logged in, and the login form if not.
Add log out functionality
1 parent aa5a3a3 commit f9d0df5

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

App.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React, { Component } from "react";
22
import { AppRegistry, View } from "react-native";
33
import firebase from "firebase";
4-
import { Header } from "./src/components/common";
4+
import { Header, Button, Spinner } from "./src/components/common";
55
import LoginForm from "./src/components/LoginForm";
66

77
export default class App extends Component {
8+
state = { loggedIn: null };
9+
810
componentWillMount() {
911
const config = {
1012
apiKey: "AIzaSyB-7ErC1c7WAmNxm6ZTufAtJpK8HP57yi8",
@@ -15,13 +17,32 @@ export default class App extends Component {
1517
messagingSenderId: "876070193723"
1618
};
1719
firebase.initializeApp(config);
20+
21+
firebase.auth().onAuthStateChanged(user => {
22+
if (user) {
23+
this.setState({ loggedIn: true });
24+
} else {
25+
this.setState({ loggedIn: false });
26+
}
27+
});
28+
}
29+
30+
renderContent() {
31+
switch (this.state.loggedIn) {
32+
case true:
33+
return <Button onPress={() => firebase.auth().signOut()}>Log Out</Button>;
34+
case false:
35+
return <LoginForm />;
36+
default:
37+
return <Spinner size="large" />;
38+
}
1839
}
1940

2041
render() {
2142
return (
2243
<View>
2344
<Header headerText="Authentication" />
24-
<LoginForm />
45+
{this.renderContent()}
2546
</View>
2647
);
2748
}

src/components/LoginForm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class LoginForm extends Component {
7777

7878
<Text style={styles.errorTextStyle}>{this.state.error}</Text>
7979

80-
<CardSection>{this.renderButton()}</CardSection>
80+
{this.renderButton()}
8181
</Card>
8282
);
8383
}

src/components/common/Header.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ const styles = {
2121
height: 60,
2222
paddingTop: 15,
2323
shadowColor: "#000",
24-
shadowOffset: {
25-
width: 0,
26-
height: 2
27-
},
24+
shadowOffset: { width: 0, height: 2 },
2825
shadowOpacity: 0.2,
2926
elevation: 2,
3027
position: "relative"

0 commit comments

Comments
 (0)