Next.js Discord

Discord Forum

Next Auth Client Fetch Error

Answered
Shawty posted this in #help-forum
Open in Discord
Avatar
ShawtyOP
code:
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
Answered by New Guinea Singing Dog
have you tried with getServerSession()?
View full answer

28 Replies

Avatar
ShawtyOP
ive tested that making a post request doesnt work and get request works
Avatar
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
Avatar
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)

    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
Avatar
New Guinea Singing Dog
Maybe send the code for getSession?
What library is that?
Avatar
ShawtyOP
getSession is from next auth?
import {getSession} from "next-auth/react";
Avatar
New Guinea Singing Dog
have you tried with getServerSession()?
Answer
Avatar
New Guinea Singing Dog
Image
Avatar
ShawtyOP
hm i never saw that lemme check
can you send me a link to that?
oh wait i found it lemme try it
Avatar
New Guinea Singing Dog
Avatar
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}`)
Avatar
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
Avatar
ShawtyOP
this lil thing?
secret: "randomkey",
Avatar
New Guinea Singing Dog
Image
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}
Avatar
ShawtyOP
ah i see, ive just put a random word in there but it works in the end. thanks
Avatar
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