adomaitis
adomaitis
TTCTheo's Typesafe Cult
Created by adomaitis on 5/14/2024 in #questions
My NextAuth session callback fetches the database too often
No description
2 replies
TTCTheo's Typesafe Cult
Created by adomaitis on 8/2/2023 in #questions
NextJS recursively call API route.ts from API route.ts
I have Site, Place, and Element in my database using Planetscale. Sites are the root, they can have Places and Elements. Places can also have Places and Elements. Elements are final. The API that deletes Sites would call the API to delete Places if any, in the current Site, and call the API to delete Elements would do the same. The Place delete API would check if there are sub-Places, and call the API for all of them, same as for Elements. So, calling the delete API on a Site would delete all of its Places and Elements. Calling the API on a Place do the same Is this doable? or should I try something else? I am getting issues with authentication when I try to fetch those APIs inside an API. Delete functions that call the APIs:
async function deletePlace(place: Place) {
try {
await fetch(`${APP_URL}/api/locais/${place.id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
console.log(`Deleted place with ID ${place.id}`);
} catch (error) {
console.error(error);
}
}
async function deletePlace(place: Place) {
try {
await fetch(`${APP_URL}/api/locais/${place.id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
console.log(`Deleted place with ID ${place.id}`);
} catch (error) {
console.error(error);
}
}
Part of the api that deletes (try) every Place/Element:
...
let places;
try {
query = await conn.execute("SELECT * FROM Place WHERE site_id = ?", [
body.data.id,
]);
places = query.rows as Place[];
} catch (e) {
console.log(e);
return ErrorResponse(...);
}
if (places.length > 0) {
const r = await Promise.all(
places.map((place) => deletePlace(place))
);
console.log(r);
}
...
let places;
try {
query = await conn.execute("SELECT * FROM Place WHERE site_id = ?", [
body.data.id,
]);
places = query.rows as Place[];
} catch (e) {
console.log(e);
return ErrorResponse(...);
}
if (places.length > 0) {
const r = await Promise.all(
places.map((place) => deletePlace(place))
);
console.log(r);
}
What happens when I call these APIs is that I am unauthenticated:
const session = await getServerSession(authOptions);
if (!session) {
console.log("unauthenticated?");
return ErrorResponse(...);
const session = await getServerSession(authOptions);
if (!session) {
console.log("unauthenticated?");
return ErrorResponse(...);
Terminal from when I run it:
unauthenticated?
Deleted place with ID 86
[ undefined ]
- ┌ DELETE /api/obras/46 200 in 1184ms
└──── DELETE http://localhost:3000/api/locais/86 403 in 310ms (cache: MISS)
unauthenticated?
Deleted place with ID 86
[ undefined ]
- ┌ DELETE /api/obras/46 200 in 1184ms
└──── DELETE http://localhost:3000/api/locais/86 403 in 310ms (cache: MISS)
I hope you can point me in the right direction. Thx
4 replies
TTCTheo's Typesafe Cult
Created by adomaitis on 3/8/2023 in #questions
Next 13 + NextAuth - Redirect on the server
So... I was wondering if anyone has this figured out. I want a protected dashboard page that gets the session and redirects if not authed. Buttt... I wanted this on a server component. Instead of rendering the page then redirecting if session is null.
import { authOptions } from "@/src/pages/api/auth/[...nextauth]";
import { unstable_getServerSession } from "next-auth/next";

export default async function DashboardPage() {
const session = await unstable_getServerSession(authOptions);
if (!session) {
// return redirect to login or whatever
}
return (
<>
<main className=" z-10 flex h-screen w-screen flex-col items-center justify-center">
<h1>we have session</h1>
</main>
</>
);
}
import { authOptions } from "@/src/pages/api/auth/[...nextauth]";
import { unstable_getServerSession } from "next-auth/next";

export default async function DashboardPage() {
const session = await unstable_getServerSession(authOptions);
if (!session) {
// return redirect to login or whatever
}
return (
<>
<main className=" z-10 flex h-screen w-screen flex-col items-center justify-center">
<h1>we have session</h1>
</main>
</>
);
}
with this I get the following error lmao
error - ./node_modules/@mapbox/node-pre-gyp/lib/clean.js:8:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
./node_modules/bcrypt/bcrypt.js
./src/pages/api/auth/[...nextauth].ts
./app/(app)/dashboard/page.tsx

https://nextjs.org/docs/messages/module-not-found
error - ./node_modules/@mapbox/node-pre-gyp/lib/clean.js:8:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
./node_modules/bcrypt/bcrypt.js
./src/pages/api/auth/[...nextauth].ts
./app/(app)/dashboard/page.tsx

https://nextjs.org/docs/messages/module-not-found
And yes, I wish I could solve this without "use client";
4 replies
TTCTheo's Typesafe Cult
Created by adomaitis on 2/16/2023 in #questions
useEffect being weir - EventListener
16 replies
TTCTheo's Typesafe Cult
Created by adomaitis on 1/17/2023 in #questions
Prisma + Planetscale TLS/SLL error
4 replies