Happy path

This commit is contained in:
Manav Rathi
2024-06-14 11:41:50 +05:30
parent dd0f7d3142
commit cc3f398a78
2 changed files with 39 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ import log from "@/next/log";
import type { AppName } from "@/next/types/app";
import { clientPackageName } from "@/next/types/app";
import { TwoFactorAuthorizationResponse } from "@/next/types/credentials";
import { ensure } from "@/utils/ensure";
import ComlinkCryptoWorker from "@ente/shared/crypto";
import { getRecoveryKey } from "@ente/shared/crypto/helpers";
import {
@@ -12,6 +13,8 @@ import {
import { CustomError } from "@ente/shared/error";
import HTTPService from "@ente/shared/network/HTTPService";
import { accountsAppURL, apiOrigin } from "@ente/shared/network/api";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import { LS_KEYS, getData, setData } from "@ente/shared/storage/localStorage";
import { getToken } from "@ente/shared/storage/localStorage/helpers";
/**
@@ -237,3 +240,35 @@ export const checkPasskeyVerificationStatus = async (
}
return TwoFactorAuthorizationResponse.parse(await res.json());
};
/**
* Extract credentials from a successful passkey verification response and save
* them to local storage for use by subsequent steps (or normal functioning) of
* the app.
*
* @param response The result of a successful
* {@link checkPasskeyVerificationStatus}.
*
* @returns the slug that we should navigate to now.
*/
export const saveCredentialsAndNavigateTo = (
response: TwoFactorAuthorizationResponse,
) => {
// This method somewhat duplicates `saveCredentialsAndNavigateTo` in the
// /passkeys/finish page.
const { id, encryptedToken, keyAttributes } = response;
setData(LS_KEYS.USER, {
...getData(LS_KEYS.USER),
encryptedToken,
id,
});
setData(LS_KEYS.KEY_ATTRIBUTES, ensure(keyAttributes));
// TODO(MR): Remove the cast.
const redirectURL = InMemoryStore.get(MS_KEYS.REDIRECT_URL) as
| string
| undefined;
InMemoryStore.delete(MS_KEYS.REDIRECT_URL);
return redirectURL ?? "/credentials";
};

View File

@@ -3,6 +3,7 @@ import log from "@/next/log";
import {
checkPasskeyVerificationStatus,
passKeySessionExpiredErrorMessage,
saveCredentialsAndNavigateTo,
} from "@ente/accounts/services/passkey";
import EnteButton from "@ente/shared/components/EnteButton";
import { apiOrigin } from "@ente/shared/network/api";
@@ -101,11 +102,8 @@ export const VerifyingPasskey: React.FC<VerifyingPasskeyProps> = ({
try {
const response =
await checkPasskeyVerificationStatus(passkeySessionID);
if (!response) {
setVerificationStatus("pending");
} else {
// TODO-PK:
}
if (!response) setVerificationStatus("pending");
else router.push(saveCredentialsAndNavigateTo(response));
} catch (e) {
log.error("Passkey verification status check failed", e);
setDialogBoxAttributesV2(
@@ -114,6 +112,7 @@ export const VerifyingPasskey: React.FC<VerifyingPasskeyProps> = ({
? sessionExpiredDialogAttributes(logout)
: genericErrorAttributes(),
);
setVerificationStatus("waiting");
}
};