Last active
February 15, 2019 21:56
-
-
Save brygrill/4832dbdf058887b6cd9d30c6d4188068 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
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