working with api routes
Unanswered
Black-headed Gull posted this in #help-forum
Black-headed GullOP
if i do this with app router, im getting 400 error
import { NextRequest, NextResponse } from "next/server";
import serverAuth from "@/lib/serverAuth";
export default async function GET(req:NextRequest, res: NextResponse) {
if(req.method != 'GET'){
return new Response('Wrong Method', {
status: 405,
})
}
try{
const { currentUser } = await serverAuth(req);
return new Response(JSON.stringify(currentUser), {
status: 200,
})
}catch(error){
console.log(error)
return new Response('Bad Request', {
status: 400,
})
}
}
but i do this with page router, everything works fine
import { NextApiRequest, NextApiResponse } from "next";
import serverAuth from "@/lib/serverAuth";
export default async function handler(req: any, res: NextApiResponse) {
try {
if (req.method !== 'GET') {
return res.status(405).end();
}
const { currentUser } = await serverAuth(req);
return res.status(200).json(currentUser);
} catch (error) {
console.log(error);
return res.status(500).end();
}
}
can someone give a simple explaination to why is that, ive used the new api (as far as i know)
thanks
import { NextRequest, NextResponse } from "next/server";
import serverAuth from "@/lib/serverAuth";
export default async function GET(req:NextRequest, res: NextResponse) {
if(req.method != 'GET'){
return new Response('Wrong Method', {
status: 405,
})
}
try{
const { currentUser } = await serverAuth(req);
return new Response(JSON.stringify(currentUser), {
status: 200,
})
}catch(error){
console.log(error)
return new Response('Bad Request', {
status: 400,
})
}
}
but i do this with page router, everything works fine
import { NextApiRequest, NextApiResponse } from "next";
import serverAuth from "@/lib/serverAuth";
export default async function handler(req: any, res: NextApiResponse) {
try {
if (req.method !== 'GET') {
return res.status(405).end();
}
const { currentUser } = await serverAuth(req);
return res.status(200).json(currentUser);
} catch (error) {
console.log(error);
return res.status(500).end();
}
}
can someone give a simple explaination to why is that, ive used the new api (as far as i know)
thanks
7 Replies
Giant panda
Read the docs thoroughly. The signature of the new route handlers has changed drastically.
As such the way you handle requests and different methods and responses did change as well.
If you want help from anyone, please provide us with minimal reproduction or at least @/lib/serverAuth code.
https://vercel.com/guides/creating-a-minimal-reproducible-example
https://vercel.com/guides/creating-a-minimal-reproducible-example
@Z4NR34L If you want help from anyone, please provide us with minimal reproduction or at least @/lib/serverAuth code.
https://vercel.com/guides/creating-a-minimal-reproducible-example
Giant panda
That is not necessary because the issue is obvious.
The code of endpoint needs to be fixed, thats right, but there can be still be an issue with serverAuth 🙂
I'm just trying to give users complex help if only that's possible :p
Indeed serverAuth could require some refactors here