-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathauthentication.component.js
46 lines (41 loc) · 1.16 KB
/
authentication.component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import AppContext from "./app-context.component.js";
import { database, firebase } from "./firebase";
import React from "react";
import context from "./context";
const withAuthentication = Component =>
class WithAuthentication extends React.Component {
constructor(props) {
super(props);
this.state = {
authUser: null,
context: {}
};
}
componentDidMount() {
context.onContextChanged(context => {
this.setState({ context });
});
firebase.auth.onAuthStateChanged(authUser => {
if (authUser) {
database.getDefaultContext(authUser.uid).then(defaultContext => {
context.setContext(defaultContext);
});
this.setState({ authUser });
} else {
this.setState({ authUser: null });
}
});
}
componentWillUnmount() {
context.removeOnContextChanged();
}
render() {
const { authUser, context } = this.state;
return (
<AppContext.Provider value={{ authUser, ...context }}>
<Component {...this.props} />
</AppContext.Provider>
);
}
};
export default withAuthentication;