Files
zitadel/apps/login/src/ui/RegisterPasskey.tsx

223 lines
5.6 KiB
TypeScript
Raw Normal View History

"use client";
2024-09-05 13:48:33 +02:00
import { registerPasskeyLink, verifyPasskey } from "@/lib/server/passkeys";
import { coerceToArrayBuffer, coerceToBase64Url } from "@/utils/base64";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { useForm } from "react-hook-form";
import Alert from "./Alert";
2024-07-15 16:00:17 +02:00
import BackButton from "./BackButton";
2024-09-05 13:48:33 +02:00
import { Button, ButtonVariants } from "./Button";
import { Spinner } from "./Spinner";
type Inputs = {};
type Props = {
sessionId: string;
2023-06-19 16:28:29 +02:00
isPrompt: boolean;
2024-04-01 14:16:12 +02:00
authRequestId?: string;
organization?: string;
};
2024-04-01 14:16:12 +02:00
export default function RegisterPasskey({
sessionId,
isPrompt,
organization,
authRequestId,
}: Props) {
2024-09-03 10:45:55 +02:00
const { handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur",
});
const [error, setError] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const router = useRouter();
2023-06-15 13:58:32 +02:00
async function submitVerify(
passkeyId: string,
passkeyName: string,
publicKeyCredential: any,
2024-05-13 16:17:12 -04:00
sessionId: string,
2023-06-15 13:58:32 +02:00
) {
setLoading(true);
2024-09-03 09:35:42 +02:00
const response = await verifyPasskey({
passkeyId,
passkeyName,
publicKeyCredential,
sessionId,
}).catch((error: Error) => {
setError(error.message);
2024-09-04 14:47:57 +02:00
setLoading(false);
2023-06-15 13:58:32 +02:00
});
setLoading(false);
return response;
}
2024-09-03 09:35:42 +02:00
async function submitRegisterAndContinue(): Promise<boolean | void> {
setLoading(true);
const resp = await registerPasskeyLink({
sessionId,
}).catch((error: Error) => {
2024-09-03 10:45:55 +02:00
setError(error.message ?? "Could not register passkey");
2024-09-03 09:35:42 +02:00
setLoading(false);
});
setLoading(false);
if (!resp) {
setError("An error on registering passkey");
return;
}
const passkeyId = resp.passkeyId;
const options: CredentialCreationOptions =
(resp.publicKeyCredentialCreationOptions as CredentialCreationOptions) ??
{};
if (!options.publicKey) {
setError("An error on registering passkey");
return;
}
options.publicKey.challenge = coerceToArrayBuffer(
options.publicKey.challenge,
"challenge",
);
options.publicKey.user.id = coerceToArrayBuffer(
options.publicKey.user.id,
"userid",
);
if (options.publicKey.excludeCredentials) {
options.publicKey.excludeCredentials.map((cred: any) => {
cred.id = coerceToArrayBuffer(
cred.id as string,
"excludeCredentials.id",
);
2024-09-03 09:35:42 +02:00
return cred;
});
}
2024-04-01 14:16:12 +02:00
2024-09-03 09:35:42 +02:00
const credentials = await navigator.credentials.create(options);
2024-04-01 14:16:12 +02:00
2024-09-03 09:35:42 +02:00
if (
!credentials ||
!(credentials as any).response?.attestationObject ||
!(credentials as any).response?.clientDataJSON ||
!(credentials as any).rawId
) {
setError("An error on registering passkey");
return;
}
2024-04-01 14:16:12 +02:00
2024-09-03 09:35:42 +02:00
const attestationObject = (credentials as any).response.attestationObject;
const clientDataJSON = (credentials as any).response.clientDataJSON;
const rawId = (credentials as any).rawId;
const data = {
id: credentials.id,
rawId: coerceToBase64Url(rawId, "rawId"),
type: credentials.type,
response: {
attestationObject: coerceToBase64Url(
attestationObject,
"attestationObject",
),
clientDataJSON: coerceToBase64Url(clientDataJSON, "clientDataJSON"),
},
};
const verificationResponse = await submitVerify(
passkeyId,
"",
data,
sessionId,
);
if (!verificationResponse) {
setError("Could not verify Passkey!");
return;
}
const params = new URLSearchParams();
if (organization) {
params.set("organization", organization);
}
if (authRequestId) {
params.set("authRequestId", authRequestId);
params.set("sessionId", sessionId);
// params.set("altPassword", ${false}); // without setting altPassword this does not allow password
// params.set("loginName", resp.loginName);
2024-09-16 12:04:43 +02:00
router.push("/passkey?" + params);
2024-09-03 09:35:42 +02:00
} else {
router.push("/accounts?" + params);
}
}
return (
<form className="w-full">
{error && (
<div className="py-4">
<Alert>{error}</Alert>
</div>
)}
<div className="mt-8 flex w-full flex-row items-center">
2023-06-19 16:28:29 +02:00
{isPrompt ? (
<Button
type="button"
variant={ButtonVariants.Secondary}
2024-04-01 14:16:12 +02:00
onClick={() => {
if (authRequestId) {
2024-08-14 10:33:46 +02:00
const params = new URLSearchParams({
authRequest: authRequestId,
});
2024-04-01 14:16:12 +02:00
2024-08-14 10:33:46 +02:00
if (sessionId) {
params.set("sessionId", sessionId);
}
if (organization) {
params.set("organization", organization);
}
router.push("/login?" + params);
} else {
const params = new URLSearchParams();
if (sessionId) {
params.append("sessionId", sessionId);
}
if (organization) {
params.append("organization", organization);
}
2024-04-01 14:16:12 +02:00
2024-08-14 10:33:46 +02:00
router.push("/signedin?" + params);
}
2024-04-01 14:16:12 +02:00
}}
2023-06-19 16:28:29 +02:00
>
skip
</Button>
) : (
2024-07-15 16:00:17 +02:00
<BackButton />
2023-06-19 16:28:29 +02:00
)}
<span className="flex-grow"></span>
<Button
type="submit"
className="self-end"
variant={ButtonVariants.Primary}
disabled={loading || !formState.isValid}
onClick={handleSubmit(submitRegisterAndContinue)}
>
{loading && <Spinner className="h-5 w-5 mr-2" />}
continue
</Button>
</div>
</form>
);
}