2023-05-16 17:34:52 +02:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import { Button, ButtonVariants } from "./Button";
|
|
|
|
|
import { TextInput } from "./Input";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import { Spinner } from "./Spinner";
|
2023-06-29 19:06:30 +02:00
|
|
|
import { AuthenticationMethodType, LoginSettings } from "@zitadel/server";
|
2023-05-16 17:34:52 +02:00
|
|
|
|
|
|
|
|
type Inputs = {
|
|
|
|
|
loginName: string;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-29 19:06:30 +02:00
|
|
|
type Props = {
|
|
|
|
|
loginSettings: LoginSettings | undefined;
|
|
|
|
|
loginName: string | undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function UsernameForm({ loginSettings, loginName }: Props) {
|
2023-05-16 17:34:52 +02:00
|
|
|
const { register, handleSubmit, formState } = useForm<Inputs>({
|
|
|
|
|
mode: "onBlur",
|
2023-06-29 19:06:30 +02:00
|
|
|
defaultValues: {
|
|
|
|
|
loginName: loginName ? loginName : "",
|
|
|
|
|
},
|
2023-05-16 17:34:52 +02:00
|
|
|
});
|
|
|
|
|
|
2023-06-29 14:54:07 +02:00
|
|
|
const router = useRouter();
|
2023-05-16 17:34:52 +02:00
|
|
|
|
2023-06-29 14:54:07 +02:00
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
2023-05-16 17:34:52 +02:00
|
|
|
|
2023-06-29 19:06:30 +02:00
|
|
|
async function submitLoginName(values: Inputs) {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
const res = await fetch("/loginnames", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
loginName: values.loginName,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error("Failed to load authentication methods");
|
|
|
|
|
}
|
|
|
|
|
return res.json();
|
|
|
|
|
}
|
2023-06-29 14:54:07 +02:00
|
|
|
|
2023-06-29 19:06:30 +02:00
|
|
|
async function setLoginNameAndGetAuthMethods(values: Inputs) {
|
|
|
|
|
return submitLoginName(values).then((response) => {
|
|
|
|
|
console.log(response);
|
|
|
|
|
if (response.authMethodTypes.length == 1) {
|
|
|
|
|
const method = response.authMethodTypes[0];
|
|
|
|
|
console.log(method);
|
|
|
|
|
// switch (method) {
|
|
|
|
|
// case AuthenticationMethodType.AUTHENTICATION_METHOD_TYPE_PASSWORD:
|
|
|
|
|
// return router.push(
|
|
|
|
|
// "/password?" +
|
|
|
|
|
// new URLSearchParams({ loginName: values.loginName })
|
|
|
|
|
// );
|
|
|
|
|
// case AuthenticationMethodType.AUTHENTICATION_METHOD_TYPE_PASSKEY:
|
|
|
|
|
// break;
|
|
|
|
|
// // return router.push(
|
|
|
|
|
// // "/passkey/login?" +
|
|
|
|
|
// // new URLSearchParams({ loginName: values.loginName })
|
|
|
|
|
// // );
|
|
|
|
|
// default:
|
|
|
|
|
// return router.push(
|
|
|
|
|
// "/password?" +
|
|
|
|
|
// new URLSearchParams({ loginName: values.loginName })
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-05-16 17:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { errors } = formState;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form className="w-full">
|
|
|
|
|
<div className="">
|
|
|
|
|
<TextInput
|
|
|
|
|
type="text"
|
|
|
|
|
autoComplete="username"
|
|
|
|
|
{...register("loginName", { required: "This field is required" })}
|
|
|
|
|
label="Loginname"
|
|
|
|
|
// error={errors.username?.message as string}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="mt-8 flex w-full flex-row items-center">
|
|
|
|
|
{/* <Button type="button" variant={ButtonVariants.Secondary}>
|
|
|
|
|
back
|
|
|
|
|
</Button> */}
|
|
|
|
|
<span className="flex-grow"></span>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="self-end"
|
|
|
|
|
variant={ButtonVariants.Primary}
|
|
|
|
|
disabled={loading || !formState.isValid}
|
2023-06-29 19:06:30 +02:00
|
|
|
onClick={handleSubmit(setLoginNameAndGetAuthMethods)}
|
2023-05-16 17:34:52 +02:00
|
|
|
>
|
|
|
|
|
{loading && <Spinner className="h-5 w-5 mr-2" />}
|
|
|
|
|
continue
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|