In progress sign in changes.

This commit is contained in:
Patrick Fic
2019-12-04 21:02:23 -07:00
parent 25d434ef10
commit b2649a25cc
8 changed files with 203 additions and 23 deletions

83
client/src/App/App.js Normal file
View File

@@ -0,0 +1,83 @@
import React from "react";
import "./App.css";
import { auth, createUserProfileDocument } from "../firebase/firebase.utils";
import { gql } from "apollo-boost";
import HeaderAppBar from "../components/header-app-bar/header-app-bar.component";
const SET_CURRENT_USER = gql`
mutation SetCurrentUser($user: User!) {
setCurrentUser(user: $user) @client
}
`;
const GET_CURRENT_USER = gql`
{
currentUser @client
}
`;
class App extends React.Component {
unsubscribeFromAuth = null;
componentDidMount() {
const { setCurrentUser } = this.props;
this.unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth => {
console.log("userAuth", userAuth);
if (userAuth) {
const token = await userAuth.getIdToken();
console.log("token", token);
const idTokenResult = await userAuth.getIdTokenResult();
const hasuraClaim =
idTokenResult.claims["https://hasura.io/jwt/claims"];
if (hasuraClaim) {
console.log("status should be in");
//setAuthState({ status: "in", user, token });
} else {
// Check if refresh is required.
const metadataRef = firebase
.database()
.ref("metadata/" + user.uid + "/refreshTime");
metadataRef.on("value", async () => {
// Force refresh to pick up the latest custom claims changes.
const token = await user.getIdToken(true);
//setAuthState({ status: "in", user, token });
console.log("status should be in");
});
}
} else {
//setAuthState({ status: "out" });
}
// if (userAuth) {
// const userRef = await createUserProfileDocument(userAuth);
// userRef.onSnapshot(snapShot => {
// setCurrentUser({
// id: snapShot.id,
// ...snapShot.data()
// });
// });
// } else {
// setCurrentUser(userAuth);
// }
});
}
componentWillUnmount() {
this.unsubscribeFromAuth();
}
render() {
return (
<div>
<HeaderAppBar />
</div>
);
}
}
export default App;