navigator, utils

This commit is contained in:
Max Peintner
2023-06-13 17:43:37 +02:00
parent 084051889b
commit 69301d789d
5 changed files with 195 additions and 19 deletions

View File

@@ -6,7 +6,8 @@ import { useForm } from "react-hook-form";
import { useRouter } from "next/navigation";
import { Spinner } from "./Spinner";
import Alert from "./Alert";
import { RegisterPasskeyResponse } from "@zitadel/server";
import { coerceToArrayBuffer, coerceToBase64Url } from "#/utils/base64";
type Inputs = {};
type Props = {
@@ -50,8 +51,120 @@ export default function RegisterPasskey({ sessionId }: Props) {
}
function submitRegisterAndContinue(value: Inputs): Promise<boolean | void> {
return submitRegister().then((resp: any) => {
return router.push(`/accounts`);
return submitRegister().then((resp: RegisterPasskeyResponse) => {
console.log(resp.publicKeyCredentialCreationOptions?.publicKey);
if (
resp.publicKeyCredentialCreationOptions &&
resp.publicKeyCredentialCreationOptions.publicKey
) {
resp.publicKeyCredentialCreationOptions.publicKey.challenge =
coerceToArrayBuffer(
resp.publicKeyCredentialCreationOptions.publicKey.challenge,
"challenge"
);
resp.publicKeyCredentialCreationOptions.publicKey.user.id =
coerceToArrayBuffer(
resp.publicKeyCredentialCreationOptions.publicKey.user.id,
"challenge"
);
if (
resp.publicKeyCredentialCreationOptions.publicKey.excludeCredentials
) {
resp.publicKeyCredentialCreationOptions.publicKey.excludeCredentials.map(
(cred: any) => {
cred.id = coerceToArrayBuffer(
cred.id as string,
"excludeCredentials.id"
);
return cred;
}
);
}
navigator.credentials
.create(resp.publicKeyCredentialCreationOptions)
.then((resp) => {
console.log(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 = JSON.stringify({
id: resp.id,
rawId: coerceToBase64Url(rawId, "rawId"),
type: resp.type,
response: {
attestationObject: coerceToBase64Url(
attestationObject,
"attestationObject"
),
clientDataJSON: coerceToBase64Url(
clientDataJSON,
"clientDataJSON"
),
},
});
const base64 = btoa(data);
return base64;
// if (this.type === U2FComponentDestination.MFA) {
// this.service
// .verifyMyMultiFactorU2F(base64, this.name)
// .then(() => {
// this.translate
// .get("USER.MFA.U2F_SUCCESS")
// .pipe(take(1))
// .subscribe((msg) => {
// this.toast.showInfo(msg);
// });
// this.dialogRef.close(true);
// this.loading = false;
// })
// .catch((error) => {
// this.loading = false;
// this.toast.showError(error);
// });
// } else if (this.type === U2FComponentDestination.PASSWORDLESS) {
// this.service
// .verifyMyPasswordless(base64, this.name)
// .then(() => {
// this.translate
// .get("USER.PASSWORDLESS.U2F_SUCCESS")
// .pipe(take(1))
// .subscribe((msg) => {
// this.toast.showInfo(msg);
// });
// this.dialogRef.close(true);
// this.loading = false;
// })
// .catch((error) => {
// this.loading = false;
// this.toast.showError(error);
// });
// }
} else {
setLoading(false);
setError("An error on registering passkey");
return null;
}
})
.catch((error) => {
console.error(error);
setLoading(false);
// setError(error);
return null;
});
}
// return router.push(`/accounts`);
});
}