2023-03-23 10:34:06 +01:00
|
|
|
import createPersistedState from 'use-persisted-state'
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up local storage state for account & token
|
|
|
|
*/
|
2023-03-24 16:33:14 +01:00
|
|
|
const usePersistedAccount = createPersistedState('fs-account')
|
|
|
|
const usePersistedToken = createPersistedState('fs-token')
|
2023-03-23 10:34:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2023-03-24 16:33:14 +01:00
|
|
|
const [account, setAccount] = usePersistedAccount(noAccount)
|
|
|
|
const [token, setToken] = usePersistedToken(null)
|
2023-03-23 10:34:06 +01:00
|
|
|
|
|
|
|
// Clear user data. This gets called when signing out
|
|
|
|
const clear = () => {
|
|
|
|
setAccount(noAccount)
|
|
|
|
setToken(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
account,
|
|
|
|
setAccount,
|
|
|
|
token,
|
|
|
|
setToken,
|
|
|
|
clear,
|
|
|
|
}
|
|
|
|
}
|