Skip to content

Instantly share code, notes, and snippets.

@alant
Last active December 3, 2019 17:25
Show Gist options
  • Save alant/898ab0becad5b5ae026dd7f69eb682a9 to your computer and use it in GitHub Desktop.
Save alant/898ab0becad5b5ae026dd7f69eb682a9 to your computer and use it in GitHub Desktop.
function Callback(props) {
const { dispatch } = React.useContext(AuthContext);
const location = useLocation();
useEffect(() => {
async function exchangeToken() {
try {
const resp = await axios.post(process.env.REACT_APP_BACKEND_URL + '/login/github', JSON.stringify({
code: values.code
}), {
headers: {
'Content-Type': 'application/json',
}
});
dispatch({
type: "LOGIN",
payload: { token: resp.data.token }
});
props.history.push("/");
} catch (error) {
console.log("=> backend error /login/github:", error);
throw error;
}
}
const values = queryString.parse(location.search)
if (values.code) {
exchangeToken();
}
}, [dispatch, location, props.history]);
return null;
}
export default Callback;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment