Skip to content

Commit

Permalink
Merge pull request #414 from Open-Earth-Foundation/fix/signup-validation
Browse files Browse the repository at this point in the history
fix: signup broken without inventory ID or callbackUrl search param
  • Loading branch information
lemilonkh committed Apr 3, 2024
2 parents 8f7fb0a + 7ab261d commit b60f89a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
32 changes: 19 additions & 13 deletions app/src/app/[lng]/auth/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
Input,
Text,
} from "@chakra-ui/react";
import { useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { Trans } from "react-i18next/TransWithoutContext";
import { logger } from "@/services/logger";
Expand All @@ -29,7 +29,6 @@ type Inputs = {
confirmPassword: string;
inviteCode: string;
acceptTerms: boolean;
inventory: string;
};

export default function Signup({
Expand All @@ -43,20 +42,27 @@ export default function Signup({
handleSubmit,
register,
setError: setFormError,
setValue,
formState: { errors, isSubmitting },
} = useForm<Inputs>();

const [error, setError] = useState("");

// extract inventory id from callbackUrl search parameter
let inventoryId: string | undefined = undefined;
const fullUrl = window.location.href;
const urlParams = new URL(fullUrl);
const params = urlParams.searchParams.get("callbackUrl");
const inventory = params?.split("/").pop();

useEffect(() => {
setValue("inventory", inventory!);
}, [setValue, inventory]);
const callbackUrl = urlParams.searchParams.get("callbackUrl");
if (callbackUrl) {
try {
const url = new URL(callbackUrl);
const callbackUrlSegments = url.pathname.split("/");
if (callbackUrlSegments.length > 2) {
inventoryId = callbackUrlSegments.pop();
}
} catch (err) {
console.error("Invalid callback url", callbackUrl);
}
}

const onSubmit: SubmitHandler<Inputs> = async (data) => {
if (data.password !== data.confirmPassword) {
Expand All @@ -70,7 +76,7 @@ export default function Signup({
try {
const res = await fetch("/api/v0/auth/register", {
method: "POST",
body: JSON.stringify(data),
body: JSON.stringify({ ...data, inventory: inventoryId }),
headers: {
"Content-Type": "application/json",
},
Expand All @@ -83,8 +89,8 @@ export default function Signup({
return;
}

const callbackUrl = `/auth/check-email?email=${data.email}&callbackUrl=${params}`;
router.push(callbackUrl);
const nextCallbackUrl = `/auth/check-email?email=${data.email}&callbackUrl=${callbackUrl}`;
router.push(nextCallbackUrl);

// TODO automatic login required?
// const loginResponse = await signIn("credentials", {
Expand Down
16 changes: 8 additions & 8 deletions app/src/app/api/v0/auth/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export const POST = apiHandler(async (req: Request) => {
role: Roles.User,
});

const inventory = await db.models.Inventory.findOne({
where: {
inventoryId: body.inventory,
},
});

const cityId = inventory?.cityId;
if (body.inventory) {
const inventory = await db.models.Inventory.findOne({
where: {
inventoryId: body.inventory,
},
});

user.addCity(cityId);
await user.addCity(inventory?.cityId);
}

return NextResponse.json({
user: {
Expand Down

0 comments on commit b60f89a

Please sign in to comment.