using auth() for route api, I got this error when deployment on vercel
Unanswered
Ring-necked Duck posted this in #help-forum
Ring-necked DuckOP
"unknown" is not a valid POST return type:
12 Replies
Ring-necked DuckOP
I'm using next-auth v5 and this is my code like
export const GET = auth(async (request) => {
if (!request.auth) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
});
but If I upgrade the code like this, error is gone
export async function GET(request: NextRequest): Promise<NextResponse> {
const isLoggedIn = await checkAuthorization();
if (!isLoggedIn) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
}
so , why I cannot use auth() higher level function using here?
thank you
export const GET = auth(async (request) => {
if (!request.auth) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
});
but If I upgrade the code like this, error is gone
export async function GET(request: NextRequest): Promise<NextResponse> {
const isLoggedIn = await checkAuthorization();
if (!isLoggedIn) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
}
so , why I cannot use auth() higher level function using here?
thank you
just call it inside your route handler
Ring-necked DuckOP
is it possible to share a guideline, how can I write it correctly?
export const GET = auth(async (request) => {
if (!request.auth) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
});
thank you
export const GET = auth(async (request) => {
if (!request.auth) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
});
thank you
@Ring-necked Duck is it possible to share a guideline, how can I write it correctly?
export const GET = auth(async (request) => {
if (!request.auth) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
});
thank you
oh wait, I just found their example which can use
auth() like you didRing-necked DuckOP
yes, this one work on dev but cannot depyloment on vercel
@Ring-necked Duck yes, this one work on dev but cannot depyloment on vercel
got this error "unknown" is not a valid POST return type:?
Ring-necked DuckOP
Type error: Route "app/api/users/route.ts"
has an invalid export:
"unknown" is not a valid GET return type:
Expected "void | Response | Promise<void | Response>", got "unknown".
has an invalid export:
"unknown" is not a valid GET return type:
Expected "void | Response | Promise<void | Response>", got "unknown".
@Ring-necked Duck Type error: Route "app/api/users/route.ts"
has an invalid export:
"unknown" is not a valid GET return type:
Expected "void | Response | Promise<void | Response>", got "unknown".
try this
export const GET = auth(async (request) => {
if (!request.auth) {
return handleUnauthorized();
}
const users = await prisma.user.findMany({
orderBy: {
name: "asc",
},
});
return NextResponse.json(users, { status: 200 });
}) as any;