Skip to content

Commit 00ad155

Browse files
authored
Merge pull request #108 from oasisprotocol/lw/persist-token
Persist token
2 parents 7ca74aa + 750abb7 commit 00ad155

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/contexts/RoflAppBackendAuth/Provider.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,26 @@ export function RoflAppBackendAuthProvider({ children }: { children: ReactNode }
1212
const { signMessageAsync } = useSignMessage()
1313
const chainId = useChainId()
1414

15-
const [token, setToken] = useState<string | null>(null)
15+
const [token, _setToken] = useState<string | null>(
16+
// TODO: possibly already expired or from another account. Currently detected by useInterval within a few seconds.
17+
window.localStorage.getItem('jwt')
18+
)
1619
const [isLoading, setIsLoading] = useState(false)
1720
const [error, setError] = useState<string | null>(null)
1821

1922
const { refetch: refetchNonce } = useGetNonce(isConnected ? address : undefined)
2023
const { mutateAsync: loginMutationAsync } = useLogin()
2124

25+
const setToken = (token: string | null) => {
26+
_setToken(token)
27+
try {
28+
if (token) window.localStorage.setItem('jwt', token)
29+
else window.localStorage.removeItem('jwt')
30+
} catch {
31+
// Ignore failures like Safari incognito
32+
}
33+
}
34+
2235
const getSiweMessage = useCallback((address: `0x${string}`, nonce: string, chainId: number): string => {
2336
const domain = PROD ? window.location.hostname : 'dev.rofl.app'
2437
const uri = `https://${domain}`

0 commit comments

Comments
 (0)