
I am refactoring hooks, breaking up the *god hook* that is useApp. I am also switching to `use-persisted-storage` as a dependency over our own useStorage implementation as that has been the source of some bugs lately and this one seems to do the job just fine so that's one less thing to worry about.
35 lines
757 B
JavaScript
35 lines
757 B
JavaScript
import createPersistedState from 'use-persisted-state'
|
|
|
|
/*
|
|
* Set up local storage state for account & token
|
|
*/
|
|
const usePersistedAccount = usePersistedState('fs-account')
|
|
const usePersistedToken = usePersistedState('fs-token')
|
|
|
|
/*
|
|
* Make it possible to always check for account.username
|
|
*/
|
|
const noAccount = { username: false }
|
|
|
|
/*
|
|
* The useAccount hook
|
|
*/
|
|
export function useAccount() {
|
|
// (persisted) State (saved to local storage)
|
|
const [account, setAccount] = usePersistedAccount()
|
|
const [token, setToken] = usePersistedToken()
|
|
|
|
// Clear user data. This gets called when signing out
|
|
const clear = () => {
|
|
setAccount(noAccount)
|
|
setToken(null)
|
|
}
|
|
|
|
return {
|
|
account,
|
|
setAccount,
|
|
token,
|
|
setToken,
|
|
clear,
|
|
}
|
|
}
|