Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[web] Redirect again button

Manav Rathi6aa810b50083
diff --git a/web/apps/accounts/src/pages/passkeys/verify.tsx b/web/apps/accounts/src/pages/passkeys/verify.tsx
index 013eaceb05..010c3bbfdf 100644
--- a/web/apps/accounts/src/pages/passkeys/verify.tsx
+++ b/web/apps/accounts/src/pages/passkeys/verify.tsx
@@ -1,5 +1,6 @@
import log from "@/next/log";
import type { TwoFactorAuthorizationResponse } from "@/next/types/credentials";
+import { ensure } from "@/utils/ensure";
import { nullToUndefined } from "@/utils/transform";
import { VerticallyCentered } from "@ente/shared/components/Container";
import EnteButton from "@ente/shared/components/EnteButton";
@@ -13,8 +14,8 @@ import {
finishPasskeyAuthentication,
isWebAuthnSupported,
isWhitelistedRedirect,
- redirectAfterPasskeyAuthentication,
redirectToPasskeyRecoverPage,
+ redirectURLWithPasskeyAuthentication,
signChallenge,
} from "services/passkey";
@@ -30,10 +31,18 @@ const Page = () => {
| "unknownRedirect" /* Unrecoverable error */
| "unrecoverableFailure" /* Unrocevorable error - generic */
| "failed" /* Recoverable error */
- | "waitingForUser"; /* ...to authenticate with their passkey */
+ | "waitingForUser" /* ...to authenticate with their passkey */
+ | "redirecting"; /* The user back to the calling app */
const [status, setStatus] = useState<Status>("loading");
+ // The URL we're redirecting to on success.
+ //
+ // This will only be set when status is "redirecting".
+ const [redirectURLWithData, setRedirectURLWithData] = useState<
+ URL | undefined
+ >();
+
/** (re)start the authentication flow */
const authenticate = async () => {
if (!isWebAuthnSupported()) {
@@ -107,12 +116,13 @@ const Page = () => {
return;
}
- // Conceptually we can `setStatus("done")` at this point, but we'll
- // leave this page anyway, so no need to tickle React.
+ setStatus("redirecting");
- await redirectAfterPasskeyAuthentication(
- redirectURL,
- authorizationResponse,
+ setRedirectURLWithData(
+ await redirectURLWithPasskeyAuthentication(
+ redirectURL,
+ authorizationResponse,
+ ),
);
};
@@ -120,6 +130,10 @@ const Page = () => {
void authenticate();
}, []);
+ useEffect(() => {
+ if (redirectURLWithData) redirectToURL(redirectURLWithData);
+ }, [redirectURLWithData]);
+
const handleRetry = () => void authenticate();
const handleRecover = () => {
@@ -133,6 +147,9 @@ const Page = () => {
redirectToPasskeyRecoverPage(new URL(recover));
};
+ const handleRedirectAgain = () =>
+ redirectToURL(ensure(redirectURLWithData));
+
const components: Record<Status, React.ReactNode> = {
loading: <Loading />,
unknownRedirect: <UnknownRedirect />,
@@ -142,6 +159,7 @@ const Page = () => {
<RetriableFailed onRetry={handleRetry} onRecover={handleRecover} />
),
waitingForUser: <WaitingForUser />,
+ redirecting: <Redirecting onRetry={handleRedirectAgain} />,
};
return components[status];
@@ -149,6 +167,11 @@ const Page = () => {
export default Page;
+const redirectToURL = (url: URL) => {
+ log.info(`Redirecting to ${url.href}`);
+ window.location.href = url.href;
+};
+
const Loading: React.FC = () => {
return (
<VerticallyCentered>
@@ -286,3 +309,45 @@ const WaitingImgContainer = styled("div")`
justify-content: center;
margin-block-start: 1rem;
`;
+
+interface RedirectingProps {
+ /** Called when the user presses the button to redirect again */
+ onRetry: () => void;
+}
+
+const Redirecting: React.FC<RedirectingProps> = ({ onRetry }) => {
+ const handleClose = window.close;
+
+ return (
+ <Content>
+ <InfoIcon color="accent" fontSize="large" />
+ <Typography variant="h3">Passkey verified</Typography>
+ <Typography color="text.muted">
+ Redirecting you back to the app...
+ </Typography>
+ <Typography color="text.muted">
+ You can close this window after the app opens.
+ </Typography>
+ <ButtonStack>
+ <EnteButton
+ onClick={handleClose}
+ fullWidth
+ color="secondary"
+ type="button"
+ variant="contained"
+ >
+ {t("CLOSE")}
+ </EnteButton>
+ <EnteButton
+ onClick={onRetry}
+ fullWidth
+ color="primary"
+ type="button"
+ variant="text"
+ >
+ Redirect again
+ </EnteButton>
+ </ButtonStack>
+ </Content>
+ );
+};
diff --git a/web/apps/accounts/src/services/passkey.ts b/web/apps/accounts/src/services/passkey.ts
index 03cbd1eff8..600f5a4be1 100644
--- a/web/apps/accounts/src/services/passkey.ts
+++ b/web/apps/accounts/src/services/passkey.ts
@@ -521,25 +521,24 @@ const authenticatorAssertionResponse = (credential: Credential) => {
};
/**
- * Redirect back to the calling app that initiated the passkey authentication
- * flow with the result of the authentication.
+ * Create a redirection URL to get back to the calling app that initiated the
+ * passkey authentication flow with the result of the authentication.
*
- * @param redirectURL The URL to redirect to. Provided by the calling app that
- * initiated the passkey authentication.
+ * @param redirectURL The base URL to redirect to. Provided by the calling app
+ * that initiated the passkey authentication.
*
* @param twoFactorAuthorizationResponse The result of
* {@link finishPasskeyAuthentication} returned by the backend.
*/
-export const redirectAfterPasskeyAuthentication = async (
+export const redirectURLWithPasskeyAuthentication = async (
redirectURL: URL,
twoFactorAuthorizationResponse: TwoFactorAuthorizationResponse,
) => {
const encodedResponse = await toB64URLSafeNoPaddingString(
JSON.stringify(twoFactorAuthorizationResponse),
);
-
redirectURL.searchParams.set("response", encodedResponse);
- window.location.href = redirectURL.href;
+ return redirectURL;
};
/**