diff --git a/apps/login/src/ui/RegisterU2F.tsx b/apps/login/src/ui/RegisterU2F.tsx index a69d7d641d4..5e6de4fd500 100644 --- a/apps/login/src/ui/RegisterU2F.tsx +++ b/apps/login/src/ui/RegisterU2F.tsx @@ -24,31 +24,12 @@ export default function RegisterU2F({ organization, authRequestId, }: Props) { - const { register, handleSubmit, formState } = useForm({ - mode: "onBlur", - }); - const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const router = useRouter(); - async function submitRegister() { - setError(""); - setLoading(true); - const response = await addU2F({ - sessionId, - }).catch((error) => { - setLoading(false); - setError(error.message); - }); - - setLoading(false); - - return response; - } - async function submitVerify( u2fId: string, passkeyName: string, @@ -71,97 +52,104 @@ export default function RegisterU2F({ return response; } - function submitRegisterAndContinue(value: Inputs): Promise { - return submitRegister().then((resp: RegisterU2FResponse) => { - const u2fId = resp.u2fId; - const options: CredentialCreationOptions = - (resp.publicKeyCredentialCreationOptions as CredentialCreationOptions) ?? - {}; - - if (options.publicKey) { - 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", - ); - return cred; - }); - } - - navigator.credentials - .create(options) - .then((resp) => { - if ( - resp && - (resp as any).response.attestationObject && - (resp as any).response.clientDataJSON && - (resp as any).rawId - ) { - const attestationObject = (resp as any).response - .attestationObject; - const clientDataJSON = (resp as any).response.clientDataJSON; - const rawId = (resp as any).rawId; - - const data = { - id: resp.id, - rawId: coerceToBase64Url(rawId, "rawId"), - type: resp.type, - response: { - attestationObject: coerceToBase64Url( - attestationObject, - "attestationObject", - ), - clientDataJSON: coerceToBase64Url( - clientDataJSON, - "clientDataJSON", - ), - }, - }; - return submitVerify(u2fId, "", data, sessionId).then(() => { - 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); - - router.push("/u2f?" + params); - } else { - router.push("/accounts?" + params); - } - }); - } else { - setLoading(false); - setError("An error on registering passkey"); - return null; - } - }) - .catch((error) => { - console.error(error); - setLoading(false); - setError(error); - - return null; - }); - } + async function submitRegisterAndContinue(): Promise { + setError(""); + setLoading(true); + const response = await addU2F({ + sessionId, + }).catch((error) => { + setLoading(false); + setError(error.message); }); - } - const { errors } = formState; + if (!response) { + setLoading(false); + setError("An error on registering passkey"); + return; + } + + const u2fId = response?.u2fId; + const options: CredentialCreationOptions = + (response?.publicKeyCredentialCreationOptions as CredentialCreationOptions) ?? + {}; + + if (options.publicKey) { + 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", + ); + return cred; + }); + } + + const resp = await navigator.credentials.create(options); + + if ( + !resp || + !(resp as any).response.attestationObject || + !(resp as any).response.clientDataJSON || + !(resp as any).rawId + ) { + setError("An error on registering passkey"); + setLoading(false); + return; + } + + const attestationObject = (resp as any).response.attestationObject; + const clientDataJSON = (resp as any).response.clientDataJSON; + const rawId = (resp as any).rawId; + + const data = { + id: resp.id, + rawId: coerceToBase64Url(rawId, "rawId"), + type: resp.type, + response: { + attestationObject: coerceToBase64Url( + attestationObject, + "attestationObject", + ), + clientDataJSON: coerceToBase64Url(clientDataJSON, "clientDataJSON"), + }, + }; + + const submitResponse = await submitVerify(u2fId, "", data, sessionId); + + if (!submitResponse) { + setLoading(false); + setError("An error on verifying 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); + + router.push("/u2f?" + params); + } else { + router.push("/accounts?" + params); + } + } + + setLoading(false); + } return (
@@ -179,8 +167,8 @@ export default function RegisterU2F({ type="submit" className="self-end" variant={ButtonVariants.Primary} - disabled={loading || !formState.isValid} - onClick={handleSubmit(submitRegisterAndContinue)} + disabled={loading} + onClick={submitRegisterAndContinue} > {loading && } continue