Skip to content

Commit

Permalink
feat: add verify recovery view on the main page
Browse files Browse the repository at this point in the history
  • Loading branch information
aon committed Feb 4, 2025
1 parent 210bfda commit d88eb60
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
48 changes: 39 additions & 9 deletions packages/auth-server/composables/useAccountLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,46 @@ export const useAccountLogin = (_chainId: MaybeRef<SupportedChainId>) => {

const { inProgress: loginInProgress, error: accountLoginError, execute: loginToAccount } = useAsync(async () => {
const client = getPublicClient({ chainId: chainId.value });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { username, address, passkeyPublicKey } = await fetchAccount(client as any, {
contracts: contractsByChain[chainId.value],
});

login({
username,
address,
passkey: toHex(passkeyPublicKey),
});
const credential = await navigator.credentials.get({
publicKey: {
challenge: new Uint8Array(32),
userVerification: "discouraged",
},
}) as PublicKeyCredential | null;
if (!credential) {
throw new Error("No credential found");
}

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { username, address, passkeyPublicKey } = await fetchAccount(client as any, {
contracts: contractsByChain[chainId.value],
uniqueAccountId: credential.id,
});

login({
username,
address,
passkey: toHex(passkeyPublicKey),
});
return { success: true } as const;
} catch {
const { checkRecoveryRequest } = useRecoveryGuardian();
const recoveryRequest = await checkRecoveryRequest(credential.id);
if (recoveryRequest) {
return {
success: false,
recoveryRequest: {
account: recoveryRequest[0],
isReady: recoveryRequest[1],
remainingTime: recoveryRequest[2],
},
} as const;
} else {
return { success: false } as const;
}
}
});

return {
Expand Down
14 changes: 14 additions & 0 deletions packages/auth-server/composables/useRecoveryGuardian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ export const useRecoveryGuardian = () => {
return tx;
});

const { inProgress: checkRecoveryRequestInProgress, error: checkRecoveryRequestError, execute: checkRecoveryRequest } = useAsync(async (accountId: string) => {
const client = getPublicClient({ chainId: defaultChain.id });
const tx = await client.readContract({
address: contractsByChain[defaultChain.id].recovery,
abi: GuardianRecoveryModuleAbi,
functionName: "checkRecoveryRequest",
args: [accountId],
});
return tx;
});

return {
confirmGuardianInProgress,
confirmGuardianError,
Expand Down Expand Up @@ -177,5 +188,8 @@ export const useRecoveryGuardian = () => {
getRecoveryInProgress,
getRecoveryError,
getRecovery,
checkRecoveryRequestInProgress,
checkRecoveryRequestError,
checkRecoveryRequest,
};
};
12 changes: 10 additions & 2 deletions packages/auth-server/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ const signUp = async () => {
navigateTo("/dashboard");
};
const logIn = async () => {
await loginToAccount();
navigateTo("/dashboard");
const result = await loginToAccount();
if (result?.success) {
navigateTo("/dashboard");
return;
}
if (result?.recoveryRequest?.isReady === false) {
navigateTo("/recovery/account-not-ready");
return;
}
// TODO: handle rest of the cases
};
</script>

0 comments on commit d88eb60

Please sign in to comment.