next-auth getToken() on an app/ POST route
Unanswered
Saltwater Crocodile posted this in #help-forum
Saltwater CrocodileOP
I'm using the /app/ based routing for next 14 and I have a POST route that I want to get the token on (for some data I've added at login). However, the body of the POST call is based on a Request object while getToken() needs a NextApiRequest object. For example:
If I use request: Request I can get the body, but not the token. If I use req: NextApiRequest I can get the token, but not the body
export async function POST(request: Request, context: { params: { id: string; }; }) {
const obj = await request.json();
const token = await getToken({ req, secret: process.env.AUTH_SECRET });
const response = await axios.post<BotDetails>('http://localhost:3010/api/bots/' + context.params.id, obj);
return Response.json(response.data);
}If I use request: Request I can get the body, but not the token. If I use req: NextApiRequest I can get the token, but not the body
10 Replies
Standard Chinchilla
try using getServerSession
return getServerSession(authOptions)@Standard Chinchilla try using getServerSession
ts
return getServerSession(authOptions)
Saltwater CrocodileOP
getServerSession(authOptions) in the nextAuth route or should I be able to call it from my own API route? Sorry, not sure where your suggesting adding it.
Standard Chinchilla
Instead of const token = …
Const session = getServerSession(authOptions)
Const session = getServerSession(authOptions)
@Standard Chinchilla Instead of const token = …
Const session = getServerSession(authOptions)
Saltwater CrocodileOP
Ok cool, will try that, thanks!
@Standard Chinchilla Instead of const token = …
Const session = getServerSession(authOptions)
Saltwater CrocodileOP
I was able to get getServerSession in my custom API route. However, it doesn't have the accessToken I need. Do I need/can I modify it with a callback to add it?
Saltwater CrocodileOP
Or alternatively can I use the serverSession in the getToken call? I see it has the cookies in there, but not sure if the JWT is there and how to access it if it is
Black-capped Chickadee
Hi
try with this
@Black-capped Chickadee try with this
Saltwater CrocodileOP
Sorry, but I'd need more context. I'm not seeing any documentation around this approach so I'm not sure what to do with it.
I have been experimenting and I have been able to get the cookies header off of the Request type that is passed in during an app/ router's custom API route. Unfortunately it doesn't seem to be a valid JWT as it returns a null when I attempt to use getToken on it....
I have been experimenting and I have been able to get the cookies header off of the Request type that is passed in during an app/ router's custom API route. Unfortunately it doesn't seem to be a valid JWT as it returns a null when I attempt to use getToken on it....
export async function POST(request: Request, context: { params: { id: string; }; }) {
const obj = await request.json();
console.log(obj);
const cookiesHeader = request.headers.get('cookie');
console.log(cookiesHeader);
const fakeReq: any = {
headers: {
cookie: cookiesHeader
}
}
const token = await getToken({ req: fakeReq, secret: process.env.AUTH_SECRET });
console.log(token);
const response = await axios.post<BotDetails>('http://localhost:3010/api/bots/' + context.params.id, obj);
return Response.json(response.data);
}