feat: session handling
This commit is contained in:
5
src/components/session/context.js
Normal file
5
src/components/session/context.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
const AuthUserContext = React.createContext(null);
|
||||||
|
|
||||||
|
export default AuthUserContext;
|
||||||
4
src/components/session/index.js
Normal file
4
src/components/session/index.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import AuthUserContext from "./context";
|
||||||
|
import withAuthentication from "./withAuthentication";
|
||||||
|
|
||||||
|
export { AuthUserContext, withAuthentication };
|
||||||
45
src/components/session/withAuthentication.js
Normal file
45
src/components/session/withAuthentication.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
import AuthUserContext from "./context";
|
||||||
|
import { withFirebase } from "../firebase";
|
||||||
|
|
||||||
|
const withAuthentication = Component => {
|
||||||
|
class WithAuthentication extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
|
||||||
|
this.state = {
|
||||||
|
authUser: JSON.parse(localStorage.getItem("authUser"))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.listener = this.props.firebase.auth.onAuthStateChanged(
|
||||||
|
authUser => {
|
||||||
|
localStorage.setItem("authUser", JSON.stringify(authUser));
|
||||||
|
this.setState({ authUser });
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
localStorage.removeItem("authUser");
|
||||||
|
this.setState({ authUser: null });
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
this.listener();
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<AuthUserContext.Provider value={this.state.authUser}>
|
||||||
|
<Component authUser={this.state.authUser} {...this.props} />
|
||||||
|
</AuthUserContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return withFirebase(WithAuthentication);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default withAuthentication;
|
||||||
Reference in New Issue
Block a user