I want to try nextjs with next-auth for authentication with the credential provider. But i always get the following error when i'm logged in and try to open a protected route:
[next-auth][error][client_fetch_error]
https://next-auth.js.org/errors#client_fetch_error session FetchError: invalid json response body at http://localhost:3000/api/auth/session reason: Unexpected token < in JSON at position 0
at C:\Users\me\workspace\vscode\projects\next-auth-typescript-example\node_modules\node-fetch\lib\index.js:272:32
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
type: 'invalid-json'
}
To reproduce the error you can do the following steps:
- Check out the official next-auth example for typescript: next-auth typescript example
- Follow the steps for the next-auth repository
- Then open the file
/pages/api/auth/[...nextauth].ts - Add the credentials provider with my code
- Start the server and try to log in trought the credentials provider with any credentials
- Try to open the protected route
- Look at your console, you will find the error
Code for the credentials provider:
Providers.Credentials({
name: "Ogame Credentials",
credentials: {
username: { label: "Username", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
const user: User = {
id: 1,
name: "J Smith",
email: "jsmith@example.com",
image: "https://google.de",
}
if (user) {
return user
} else {
return null
}
},
}),
This code is from the examples from the next-auth repo too.