1
0
Fork 0
freesewing/sites/org/hooks/use-account.mjs
Joost De Cock d7fc8282b3 chore(org): Create the use-account hook
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.
2023-03-23 10:34:06 +01:00

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,
}
}