Skip to content

Commit

Permalink
auto sign in on relayer restart
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Feb 10, 2024
1 parent d4c7dd3 commit d3611c9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions trade.renegade.fi/contexts/Renegade/renegade-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ function RenegadeProvider({ children }: React.PropsWithChildren) {
}
}, [accountId])

// TODO: This logic should probably be moved to SDK
const [seed, setSeed] = useLocalStorage<string | undefined>('seed', undefined)
const attemptedAutoSignin = React.useRef<boolean>(false)
const attemptedAutoSignin = React.useRef<AccountId>()
const initAccount = React.useCallback(async () => {
if (!seed || attemptedAutoSignin.current || accountId) return
try {
attemptedAutoSignin.current = true
console.log("Initializing account using saved seed: ", seed)
const keychain = new Keychain({ seed })
const _accountId = renegade.registerAccount(keychain)
attemptedAutoSignin.current = _accountId
// TODO: Will try to automatically create new wallet if relayer restarted
await renegade.task
.initializeAccount(_accountId)
.then(([taskId, taskJob]) => {
if (taskId !== "DONE") {
throw new Error("Account was not registered with Relayer before")
}
setTask(taskId, TaskType.InitializeAccount)
return taskJob
})
.then(() => {
Expand All @@ -115,7 +115,9 @@ function RenegadeProvider({ children }: React.PropsWithChildren) {
console.error("Tried to automatically sign in user, but failed with error: ", error)
setAccountId(undefined)
setSeed(undefined)
// TODO: If auto register doesn't work, should unregister from SDK so user can manualy register
if (attemptedAutoSignin.current) {
await renegade.unregisterAccount(attemptedAutoSignin.current)
}
}
}, [accountId, seed, setAccountId, setSeed])

Expand Down Expand Up @@ -149,7 +151,7 @@ function RenegadeProvider({ children }: React.PropsWithChildren) {
return taskJob
})
.then(() => {
attemptedAutoSignin.current = true
attemptedAutoSignin.current = accountId
setAccountId(accountId)
refreshAccount(accountId)
})
Expand Down

0 comments on commit d3611c9

Please sign in to comment.