show user creation error

This commit is contained in:
Max Peintner
2023-06-21 14:06:19 +02:00
parent 98fee5cb2b
commit 05314c7a49
3 changed files with 30 additions and 15 deletions

View File

@@ -10,7 +10,7 @@ export default function Error({ error, reset }: any) {
}, [error]); }, [error]);
return ( return (
<Boundary labels={["Home page Error UI"]} color="red"> <Boundary labels={["Login Error"]} color="red">
<div className="space-y-4"> <div className="space-y-4">
<div className="text-sm text-red-500 dark:text-red-500"> <div className="text-sm text-red-500 dark:text-red-500">
<strong className="font-bold">Error:</strong> {error?.message} <strong className="font-bold">Error:</strong> {error?.message}

View File

@@ -6,13 +6,19 @@ export async function POST(request: NextRequest) {
if (body) { if (body) {
const { email, password, firstName, lastName } = body; const { email, password, firstName, lastName } = body;
const userId = await addHumanUser(server, { return addHumanUser(server, {
email: email, email: email,
firstName, firstName,
lastName, lastName,
password: password ? password : undefined, password: password ? password : undefined,
}); })
.then((userId) => {
return NextResponse.json({ userId }); return NextResponse.json({ userId });
})
.catch((error) => {
console.log(error);
return NextResponse.json(error, { status: 500 });
});
} else { } else {
return NextResponse.error(); return NextResponse.error();
} }

View File

@@ -14,6 +14,7 @@ import {
} from "#/utils/validators"; } from "#/utils/validators";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { Spinner } from "./Spinner"; import { Spinner } from "./Spinner";
import Alert from "./Alert";
type Inputs = type Inputs =
| { | {
@@ -40,6 +41,7 @@ export default function SetPasswordForm({
}); });
const [loading, setLoading] = useState<boolean>(false); const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string>("");
const router = useRouter(); const router = useRouter();
@@ -59,7 +61,8 @@ export default function SetPasswordForm({
}); });
setLoading(false); setLoading(false);
if (!res.ok) { if (!res.ok) {
throw new Error("Failed to register user"); const error = await res.json();
throw new Error(error.details);
} }
return res.json(); return res.json();
} }
@@ -68,7 +71,6 @@ export default function SetPasswordForm({
loginName: string, loginName: string,
password: string password: string
) { ) {
setLoading(true);
const res = await fetch("/session", { const res = await fetch("/session", {
method: "POST", method: "POST",
headers: { headers: {
@@ -80,7 +82,6 @@ export default function SetPasswordForm({
}), }),
}); });
setLoading(false);
if (!res.ok) { if (!res.ok) {
throw new Error("Failed to set user"); throw new Error("Failed to set user");
} }
@@ -88,12 +89,20 @@ export default function SetPasswordForm({
} }
function submitAndLink(value: Inputs): Promise<boolean | void> { function submitAndLink(value: Inputs): Promise<boolean | void> {
return submitRegister(value).then((humanResponse: any) => { return submitRegister(value)
return createSessionWithLoginNameAndPassword(email, value.password).then( .then((humanResponse: any) => {
() => { setError("");
return createSessionWithLoginNameAndPassword(
email,
value.password
).then(() => {
setLoading(false);
return router.push(`/verify?userID=${humanResponse.userId}`); return router.push(`/verify?userID=${humanResponse.userId}`);
} });
); })
.catch((errorDetails: Error) => {
setLoading(false);
setError(errorDetails.message);
}); });
} }
@@ -102,8 +111,6 @@ export default function SetPasswordForm({
const watchPassword = watch("password", ""); const watchPassword = watch("password", "");
const watchConfirmPassword = watch("confirmPassword", ""); const watchConfirmPassword = watch("confirmPassword", "");
const [tosAndPolicyAccepted, setTosAndPolicyAccepted] = useState(false);
const hasMinLength = const hasMinLength =
passwordComplexitySettings && passwordComplexitySettings &&
watchPassword?.length >= passwordComplexitySettings.minLength; watchPassword?.length >= passwordComplexitySettings.minLength;
@@ -157,6 +164,8 @@ export default function SetPasswordForm({
/> />
)} )}
{error && <Alert>{error}</Alert>}
<div className="mt-8 flex w-full flex-row items-center justify-between"> <div className="mt-8 flex w-full flex-row items-center justify-between">
<Button type="button" variant={ButtonVariants.Secondary}> <Button type="button" variant={ButtonVariants.Secondary}>
back back