auth syncing

This commit is contained in:
Ishaan Dey 2024-04-17 22:55:02 -04:00
parent a53cc57b24
commit 14d76c605e
2 changed files with 42 additions and 24 deletions

View File

@ -20,16 +20,33 @@ export default {
const db = drizzle(env.DB); const db = drizzle(env.DB);
if (path === "/api/user" && method === "GET") { if (path === "/api/user") {
const params = url.searchParams; if (method === "GET") {
const params = url.searchParams;
if (params.has("id")) { if (params.has("id")) {
const id = params.get("id") as string; const id = params.get("id") as string;
const res = await db.select().from(user).where(eq(user.id, id)).get(); const res = await db.select().from(user).where(eq(user.id, id)).get();
return json(res ?? {}); console.log(res);
return json(res ?? {});
} else {
const res = await db.select().from(user).all();
return json(res ?? {});
}
} else if (method === "POST") {
const userSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email(),
});
const body = await request.json();
const { id, name, email } = userSchema.parse(body);
const res = await db.insert(user).values({ id, name, email }).returning().get();
return json({ res });
} else { } else {
const res = await db.select().from(user).all(); return new Response("Method Not Allowed", { status: 405 });
return new Response(JSON.stringify(res));
} }
} else return new Response("Not Found", { status: 404 }); } else return new Response("Not Found", { status: 404 });
}, },

View File

@ -12,23 +12,24 @@ export default async function AppAuthLayout({
redirect("/") redirect("/")
} }
const dbUser = await fetch(`http://localhost:8787/user?id=${user.id}`) const dbUser = await fetch(`http://localhost:8787/api/user?id=${user.id}`)
// const dbUserJSON = await dbUser.json() const dbUserJSON = await dbUser.json()
console.log(dbUser) if (!dbUserJSON?.id) {
const res = await fetch("http://localhost:8787/api/user", {
// if (!dbUserJSON) { method: "POST",
// const res = await fetch("http://localhost:8787/user", { headers: {
// method: "POST", "Content-Type": "application/json",
// headers: { },
// "Content-Type": "application/json", body: JSON.stringify({
// }, id: user.id,
// body: JSON.stringify({ name: user.firstName + " " + user.lastName,
// id: user.id, email: user.emailAddresses[0].emailAddress,
// email: user.emailAddresses[0].emailAddress, }),
// }), })
// }) } else {
// } // user already exists in db
}
return <>{children}</> return <>{children}</>
} }