Last active
June 21, 2019 14:10
-
-
Save luandevpro/37c63a0009f42eba1dd0c3627c5bdca5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
function Login() { | |
useEffect( | |
() => | |
auth.onAuthStateChanged(async user => { | |
if (user) { | |
const metadataRef = await database.ref(`metadata/${user.uid}/refreshTime`); | |
const callback = () => { | |
user.getIdToken(true); | |
}; | |
metadataRef.on('value', callback); | |
const token = await user.getIdToken(); | |
const idTokenResult = await user.getIdTokenResult(); | |
const hasuraClaim = await idTokenResult.claims['https://hasura.io/jwt/claims']; | |
if (hasuraClaim) { | |
const initialApolloGraph = initialApollo(token); | |
initialApolloGraph | |
.query({ | |
query: getUser, | |
variables: { | |
id: user.uid, | |
}, | |
}) | |
.then(users => { | |
if (users.data.user_by_pk !== null) { | |
console.log(users); | |
} else { | |
initialApolloGraph | |
.mutate({ | |
mutation: addUser, | |
variables: { | |
objects: [ | |
{ | |
id: user.uid, | |
email: user.email, | |
displayName: user.displayName, | |
photoURL: user.photoURL, | |
}, | |
], | |
}, | |
context: {}, | |
}) | |
.then(() => console.log('success')) | |
.catch(result => console.log(result)); | |
} | |
}) | |
.catch(err => { | |
console.log(err); | |
}); | |
} | |
} | |
}), | |
[] | |
); | |
const signinWithGoogle = async () => { | |
try { | |
await auth.signInWithPopup(googleAuthProvider); | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
const signOut = async () => { | |
try { | |
await auth.signOut(); | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
return ( | |
<div> | |
<button onClick={signinWithGoogle}>login</button> | |
<button onClick={signOut}>Sign out</button> | |
</div> | |
); | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment