Skip to content

Instantly share code, notes, and snippets.

@brygrill
Last active February 15, 2019 21:56
Show Gist options
  • Save brygrill/4832dbdf058887b6cd9d30c6d4188068 to your computer and use it in GitHub Desktop.
Save brygrill/4832dbdf058887b6cd9d30c6d4188068 to your computer and use it in GitHub Desktop.
const App = () => {
// state
const [authed, setAuthed] = useState(false);
const [token, setToken] = useState(null);
const [user, setUser] = useState(null);
// functions
const removeHash = () => {
window.history.pushState('', document.title, window.location.pathname);
};
const extractToken = () => {
// drop # from string
const hash = qs.parse(window.location.hash.slice(1));
// extract token and username
if (hash.access_token && hash.username) {
setAuthed(true);
setToken(hash.access_token);
setUser(hash.username);
}
// clear hash
if (hash) removeHash();
};
// lifecycle
useEffect(() => {
extractToken();
}, []);
// render
if (!authed) {
return <SignIn />;
}
return <Items token={token} user={user} />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment