K
Kinde10mo ago
zhefciad

Custom Success route.ts failing in build process && not showing logs inside GET()

Refer to the code on the thread while reading this: goal: create copy of user in postgres db after successful signup (1st code below) on local dev, it's creating users successfully as I dont need to npm run build. But for production, I need to run npm run build. I'm using a VPS to deploy this NextJS app, and the error obviously happens because it's trying to get a user on success when it shouldnt since it's in a build. What I tried to do is skipping it with an env variable that if true, skips the user authentication (second code below). And I switch it to false before npm run start. I thought it's all good and done, but when I try to sign up a user, it is not getting created in my strapi database. And for some reason, the console logs anywhere inside the GET() is not showing in the console nor any errors. I know this because the logs are only TEST 1 TEST 2 which are the first two logs outside the GET() (2st code below) My guess is it's running because it's redirecting me to my baseFrontendUrl, and the console and error logs are just invisible. The code works locally, so I'm guessing the skipping stuff I did on npm run build is causing the issue. How do i debug this? How do I make the errors/logs show inside the GET()? Or is there another way of doing it?
5 Replies
zhefciad
zhefciadOP10mo ago
Logs from npm run build on prod:
./src/app/api/auth/success/route.ts:30:59
Type error: 'user' is possibly 'null'.

28 | console.log("new user created")
29 | const dbUserResponse = await axios.get(
> 30 | `${baseUrl}/api/kinde-users?filters[kindeID][$eq]=${user.id}`
| ^
31 | );
32 | console.log("dbUserResponse:", dbUserResponse.data);
33 | console.log("TEST 6");
Linting and checking validity of types ...root@srv478287:/var/www/reelist8-master/frontend/src/app/api/auth/success#
./src/app/api/auth/success/route.ts:30:59
Type error: 'user' is possibly 'null'.

28 | console.log("new user created")
29 | const dbUserResponse = await axios.get(
> 30 | `${baseUrl}/api/kinde-users?filters[kindeID][$eq]=${user.id}`
| ^
31 | );
32 | console.log("dbUserResponse:", dbUserResponse.data);
33 | console.log("TEST 6");
Linting and checking validity of types ...root@srv478287:/var/www/reelist8-master/frontend/src/app/api/auth/success#
src > app > api > auth > success > route.ts:
src > app > api > auth > success > route.ts:
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server"; import { NextResponse } from "next/server"; import axios from "axios"; console.log("TEST 1"); const baseUrl = process.env.NEXT_PUBLIC_BASE_URL "http://127.0.0.1:1337"; const baseFrontendUrl = process.env.NEXT_PUBLIC_FRONTEND_URL "http://localhost:3000"; console.log("TEST 2"); export async function GET() { const { getUser } = getKindeServerSession(); const user = await getUser(); console.log("User from Kinde:", user); console.log("TEST 3"); if (process.env.NEXT_PUBLIC_BUILD_TIME) { console.log("Skipping user authentication during build."); return NextResponse.redirect(baseFrontendUrl); } console.log("TEST 4"); if (!user user == null !user.id) throw new Error( "Something went wrong with authentication: " + JSON.stringify(user) ); console.log("TEST 5"); try { const dbUserResponse = await axios.get( ${baseUrl}/api/kinde-users?filters[kindeID][$eq]=${user.id} ); console.log("dbUserResponse:", dbUserResponse.data); console.log("TEST 6"); if (!dbUserResponse.data.data || dbUserResponse.data.data.length === 0) { const newUserResponse = await axios.post(baseUrl + "/api/kinde-users", { data: { kindeID: user.id, firstName: user.given_name ?? "", lastName: user.family_name ?? "", email: user.email ?? "", picture: user.picture ?? "", }, }); console.log("New user created:", newUserResponse.data); } console.log("TEST 7"); return NextResponse.redirect(baseFrontendUrl); } catch (error) { console.log("TEST 8"); console.error("Error interacting with Strapi:", error); throw new Error("Failed to interact with Strapi"); } } ```
onderay
onderay10mo ago
Thanks for providing detailed information into the problem you are facing. I will need to check our NextJS team members on the best way to solve this for you as your build process is not what I normally see.
zhefciad
zhefciadOP10mo ago
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { NextResponse } from "next/server";
import axios from "axios";


const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://127.0.0.1:1337";
const baseFrontendUrl =
process.env.NEXT_PUBLIC_FRONTEND_URL || "http://localhost:3000";

console.log("Test 1")

GET();

export async function GET() {
console.log("Test 2")
const { getUser } = getKindeServerSession();
const user = await getUser();
console.log("User from Kinde:", user);

try {
if (user) {
console.log("Test 3")
const dbUserResponse = await axios.get(
`${baseUrl}/api/kinde-users?filters[kindeID][$eq]=${user.id}`
);
console.log("dbUserResponse:", dbUserResponse.data);

if (!dbUserResponse.data.data || dbUserResponse.data.data.length === 0) {
console.log("Test 4")
const newUserResponse = await axios.post(baseUrl + "/api/kinde-users", {
data: {
kindeID: user.id,
firstName: user.given_name ?? "",
lastName: user.family_name ?? "",
email: user.email ?? "",
picture: user.picture ?? "",
},
});
console.log("New user created:", newUserResponse.data);
}
}

return NextResponse.redirect(baseFrontendUrl);
} catch (error) {
console.error("Error interacting with Strapi:", error);
throw new Error("Failed to interact with Strapi");
}
}
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { NextResponse } from "next/server";
import axios from "axios";


const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://127.0.0.1:1337";
const baseFrontendUrl =
process.env.NEXT_PUBLIC_FRONTEND_URL || "http://localhost:3000";

console.log("Test 1")

GET();

export async function GET() {
console.log("Test 2")
const { getUser } = getKindeServerSession();
const user = await getUser();
console.log("User from Kinde:", user);

try {
if (user) {
console.log("Test 3")
const dbUserResponse = await axios.get(
`${baseUrl}/api/kinde-users?filters[kindeID][$eq]=${user.id}`
);
console.log("dbUserResponse:", dbUserResponse.data);

if (!dbUserResponse.data.data || dbUserResponse.data.data.length === 0) {
console.log("Test 4")
const newUserResponse = await axios.post(baseUrl + "/api/kinde-users", {
data: {
kindeID: user.id,
firstName: user.given_name ?? "",
lastName: user.family_name ?? "",
email: user.email ?? "",
picture: user.picture ?? "",
},
});
console.log("New user created:", newUserResponse.data);
}
}

return NextResponse.redirect(baseFrontendUrl);
} catch (error) {
console.error("Error interacting with Strapi:", error);
throw new Error("Failed to interact with Strapi");
}
}
I run the GET() manually and it somehow worked, I can now see console logs after success inside the GET() function. The problem is it's not getting any user. This is the log
Test 1
Test 2
User from Kinde: null
Test 1
Test 2
User from Kinde: null
I solved it by moving the user creation logic in the frontend. But if there's any way to build the file as a server side code without any errors, please let me know.
onderay
onderay10mo ago
Great to hear! I will still get a more experienced team member to provide you some advice.
Oli - Kinde
Oli - Kinde10mo ago
Hey @zhefciad Would it be possible to get a small reproducible example code that with your issue, so we can try reproduce the issue on our end?
Want results from more Discord servers?
Add your server