Next Auth Client Fetch Error
Answered
Shawty posted this in #help-forum
ShawtyOP
code:
error:
theres nothing wrong with my setup cause everything else works but it only doesnt work when i make a post request. it works fine on get request tho
export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method !== "POST") return res.status(401).json({error: 'Unauthorized Method'});
const session = await getSession({
req: req,
});
}
error:
[next-auth][error][CLIENT_FETCH_ERROR]
https://next-auth.js.org/errors#client_fetch_error undefined {
error: {},
url: 'http://localhost:3000/api/auth/session',
message: undefined
}
theres nothing wrong with my setup cause everything else works but it only doesnt work when i make a post request. it works fine on get request tho
28 Replies
ShawtyOP
ive tested that making a post request doesnt work and get request works
New Guinea Singing Dog
are you returning a nextApiResponse?
It looks like you're just another fetch method to get information about the session
but you're not returning the session data back to the front end etc
ShawtyOP
ive used a API tool and tried both get and post methods:
for get, it works and i get the session,
for post, i get 400 (Bad Request)
for get, it works and i get the session,
for post, i get 400 (Bad Request)
const session = await getSession({
req:req
});
console.log(`Session exists: ${!!session}`)
thats not the error rn. im just testing to see if the session works in the first place
i cut out the code for convience
New Guinea Singing Dog
Maybe send the code for getSession?
What library is that?
ShawtyOP
getSession is from next auth?
import {getSession} from "next-auth/react";
New Guinea Singing Dog
have you tried with getServerSession()?
Answer
New Guinea Singing Dog
ShawtyOP
hm i never saw that lemme check
can you send me a link to that?
oh wait i found it lemme try it
New Guinea Singing Dog
ShawtyOP
now i get this:
[next-auth][error][JWT_SESSION_ERROR]
https://next-auth.js.org/errors#jwt_session_error decryption operation failed {
message: 'decryption operation failed',
stack: 'JWEDecryptionFailed: decryption operation failed\n' +
' at gcmDecrypt (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\jose\\dist\\node\\cjs\\runtime\\decrypt.js:67:15)\n' +
' at decrypt (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\jose\\dist\\node\\cjs\\runtime\\decrypt.js:92:20)\n' +
' at flattenedDecrypt (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\jose\\dist\\node\\cjs\\jwe\\flattened\\decrypt.js:143:52)\n' +
' at async compactDecrypt (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\jose\\dist\\node\\cjs\\jwe\\compact\\decrypt.js:18:23)\n' +
' at async jwtDecrypt (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\jose\\dist\\node\\cjs\\jwt\\decrypt.js:8:23)\n' +
' at async Object.decode (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\next-auth\\jwt\\index.js:64:7)\n' +
' at async Object.session (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\next-auth\\core\\routes\\session.js:43:28)\n' +
' at async AuthHandler (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\next-auth\\core\\index.js:165:27)\n' +
' at async getServerSession (C:\\Users\\kobel\\WebstormProjects\\dashboard\\node_modules\\next-auth\\next\\index.js:158:19)\n' +
' at async __WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///(api)/./src/pages/api/user/[id]/data/index.tsx:16:21)',
name: 'JWEDecryptionFailed'
}
code :
export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method !== "POST") return res.status(401).json({error: 'Unauthorized Method'});
const session = await getServerSession(req, res, {});
console.log(`Session exists: ${!!session}`)
New Guinea Singing Dog
I think you'll also need to pass your auth secret into auth options
do you have it in .env?
its making the request but unable to decrypt it so its throwing an error
ShawtyOP
this lil thing?
secret: "randomkey",
New Guinea Singing Dog
Yeah just try and provide it to the options
Because i think it'll have to be explicity called NEXTAUTH_SECRET in your .env files for it to infer the value but im not entirely sure about it, i would just try and explicity pass it to the options by doing like
{secret: process.env.NEXTAUTH_SECRET}
ShawtyOP
ah i see, ive just put a random word in there but it works in the end. thanks
New Guinea Singing Dog
Oh yep i see, nw, good practice to also never put the plain value into your server code, always use .env