Skip to content

Commit a20ec71

Browse files
committed
13-Manage Users with Firebase's Realtime Database in React
1 parent 7709484 commit a20ec71

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

src/components/Admin/index.js

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,70 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22

3-
const Admin = () => (
4-
<div>
5-
<h1>Admin</h1>
6-
</div>
3+
import { withFirebase } from '../Firebase';
4+
5+
class AdminPage extends Component {
6+
constructor(props) {
7+
super(props);
8+
9+
this.state = {
10+
loading: false,
11+
users: [],
12+
};
13+
}
14+
15+
componentDidMount() {
16+
this.setState({ loading: true });
17+
18+
this.props.firebase.users().on('value', snapshot => {
19+
const usersObject = snapshot.val();
20+
21+
const usersList = Object.keys(usersObject).map(key => ({
22+
...usersObject[key],
23+
uid: key,
24+
}));
25+
26+
this.setState({
27+
users: usersList,
28+
loading: false,
29+
});
30+
});
31+
}
32+
33+
componentWillUnmount() {
34+
this.props.firebase.users().off();
35+
}
36+
37+
render() {
38+
const { users, loading } = this.state;
39+
40+
return (
41+
<div>
42+
<h1>Admin</h1>
43+
44+
{loading && <div>Loading ...</div>}
45+
46+
<UserList users={users} />
47+
</div>
48+
);
49+
}
50+
}
51+
52+
const UserList = ({ users }) => (
53+
<ul>
54+
{users.map(user => (
55+
<li key={user.uid}>
56+
<span>
57+
<strong>ID:</strong> {user.uid}
58+
</span>
59+
<span>
60+
<strong>E-Mail:</strong> {user.email}
61+
</span>
62+
<span>
63+
<strong>Username:</strong> {user.username}
64+
</span>
65+
</li>
66+
))}
67+
</ul>
768
);
869

9-
export default Admin;
70+
export default withFirebase(AdminPage);

src/components/Navigation/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const NavigationAuth = () => (
2727
<li>
2828
<Link to={ROUTES.ACCOUNT}>Account</Link>
2929
</li>
30+
<li>
31+
<Link to={ROUTES.ADMIN}>Admin</Link>
32+
</li>
3033
<li>
3134
<SignOutButton />
3235
</li>

src/components/SignUp/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@ class SignUpFormBase extends Component {
3232
this.props.firebase
3333
.doCreateUserWithEmailAndPassword(email, passwordOne)
3434
.then(authUser => {
35-
this.setState({ ...INITIAL_STATE });
36-
this.props.history.push(ROUTES.HOME);
35+
// Create a user in your Firebase realtime database
36+
this.props.firebase
37+
.user(authUser.user.uid)
38+
.set({
39+
username,
40+
email,
41+
})
42+
.then(() => {
43+
this.setState({ ...INITIAL_STATE });
44+
this.props.history.push(ROUTES.HOME);
45+
})
46+
.catch(error => {
47+
this.setState({ error });
48+
});
3749
})
3850
.catch(error => {
3951
this.setState({ error });

0 commit comments

Comments
 (0)