CP3
CP3
TTCTheo's Typesafe Cult
Created by CP3 on 8/12/2023 in #questions
Prisma postgres DB not updating
Hello guys. I'm working on auth for my nextjs app built with nextauth and prisma. Initially, I created the project using a local sqlite db and everything worked properly. But when I switched to a postgres db with railway.app it stopped working properly. When I make a post request to create a new user account from Insomnia(an API Client), the db updates with the new user. But when I make that same request through the sign up form UI, the db doesn't update even after the fetch request returns an ok response. This is my signup function:
const onSubmit = async (data: FormData) => {
try {
const apiUrl = process.env.NEXTAUTH_URL;
const response = await fetch(`${apiUrl}/api/user`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: data.email,
password: data.password,
}),
});

if (response.ok) {
console.log("User created successfully!");
setConfirm(true);
// Handle success or redirect to another page if needed
} else {
console.error("Failed to create user.");
// Handle errors, show error messages, etc.
}
} catch (error) {
console.error("Error occurred:", error);
// Handle network errors or other exceptions
}
};
const onSubmit = async (data: FormData) => {
try {
const apiUrl = process.env.NEXTAUTH_URL;
const response = await fetch(`${apiUrl}/api/user`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: data.email,
password: data.password,
}),
});

if (response.ok) {
console.log("User created successfully!");
setConfirm(true);
// Handle success or redirect to another page if needed
} else {
console.error("Failed to create user.");
// Handle errors, show error messages, etc.
}
} catch (error) {
console.error("Error occurred:", error);
// Handle network errors or other exceptions
}
};
This is my api/user/route.ts file:
import prisma from "@/lib/prisma";
import * as bcrypt from "bcrypt";
import { NextRequest } from "next/server";

interface RequestBody {
email: string;
password: string;
}
export async function POST(request: Request | NextRequest) {
const body: RequestBody = await request?.json();

const user = await prisma.user.create({
data: {
email: body.email,
password: await bcrypt.hash(body.password, 10),
},
});

const { password, ...result } = user;
return new Response(JSON.stringify(result));
}
import prisma from "@/lib/prisma";
import * as bcrypt from "bcrypt";
import { NextRequest } from "next/server";

interface RequestBody {
email: string;
password: string;
}
export async function POST(request: Request | NextRequest) {
const body: RequestBody = await request?.json();

const user = await prisma.user.create({
data: {
email: body.email,
password: await bcrypt.hash(body.password, 10),
},
});

const { password, ...result } = user;
return new Response(JSON.stringify(result));
}
4 replies