Environment
System:
OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
CPU: (2) x64 Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
Memory: 1.94 GB / 3.82 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 20.3.0 - ~/nvm/current/bin/node
Yarn: 1.22.19 - /usr/bin/yarn
npm: 9.6.7 - ~/nvm/current/bin/npm
pnpm: 8.6.2 - ~/nvm/current/bin/pnpm
Reproduction URL
https://github.com/muckee/NextAuth-cannot-access-query-prop-of-res-object
Describe the issue
When attempting to follow the NextAuth documentation to implement advanced initalisation using the Next.js AppRouter, it is not possible to implement conditions within the auth route, because res.query does not exist.
My implementation is as follows:
const auth = async (
req,
res,
) => {
const authOptions = {
providers: [
discordProvider,
],
session: {
strategy: "jwt",
},
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async session({ session, token }) {
// @ts-ignore
session.publicKey = token.sub;
if (session.user) {
session.user.name = token.sub;
session.user.image = `https://ui-avatars.com/api/?name=${token.sub}&background=random`;
}
return session;
},
}
};
// ERROR: `req does not have a property named 'query'`
// Comment out lines 39 - 43 and there will not be an error
const isDefaultSigninPage =
req.method === "GET" && req.query.nextauth.includes("signin");
if (isDefaultSigninPage) {
providers.pop();
}
return await NextAuth(req, res, authOptions);
}
export {
auth as GET,
auth as POST,
};
I've tried looking through the res object to see if the properties have been renamed, but couldn't see anything.
Other people with the issue have reverted to using the pages/ folder, but I want to find a solution which uses AppRouter.
I've also seen some people mention the issue occurring when they use getServerSideProps(), but of course that isn't relevant here.
I've reproduced the issue in a GitHub codespace based on the next-auth-example template and attached a link to the result
How to reproduce
- Create a new Next.js application with
npx create-next-app ... or yarn dlx create-next-app.
- Ensure that env vars are set properly.
- Follow the NextAuth documentation to implement the NextAuth route at
src/app/api/auth/[...nextauth]/route.js.
- Attempt to add a condition which checks whether
req.query.nextauth includes 'signin', following the pattern described in the official documentation.
Provided that the application is properly configured and the file structure adheres to the AppRouter pattern (does not use the pages/ folder), this minimal setup should throw an error whenever the user attempts to visit any page which attempts to load an existing session, or click on a button which implements the 'signIn' function.
Expected behavior
The app should function properly and the req object should contain a query prop, or the NextAuth documentation should be updated to include whatever migration steps must be made.
Environment
System:
OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
CPU: (2) x64 Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
Memory: 1.94 GB / 3.82 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 20.3.0 - ~/nvm/current/bin/node
Yarn: 1.22.19 - /usr/bin/yarn
npm: 9.6.7 - ~/nvm/current/bin/npm
pnpm: 8.6.2 - ~/nvm/current/bin/pnpm
Reproduction URL
https://github.com/muckee/NextAuth-cannot-access-query-prop-of-res-object
Describe the issue
When attempting to follow the NextAuth documentation to implement advanced initalisation using the Next.js AppRouter, it is not possible to implement conditions within the auth route, because
res.querydoes not exist.My implementation is as follows:
I've tried looking through the
resobject to see if the properties have been renamed, but couldn't see anything.Other people with the issue have reverted to using the
pages/folder, but I want to find a solution which uses AppRouter.I've also seen some people mention the issue occurring when they use
getServerSideProps(), but of course that isn't relevant here.I've reproduced the issue in a GitHub codespace based on the
next-auth-exampletemplate and attached a link to the resultHow to reproduce
npx create-next-app ...oryarn dlx create-next-app.src/app/api/auth/[...nextauth]/route.js.req.query.nextauthincludes 'signin', following the pattern described in the official documentation.Provided that the application is properly configured and the file structure adheres to the AppRouter pattern (does not use the
pages/folder), this minimal setup should throw an error whenever the user attempts to visit any page which attempts to load an existing session, or click on a button which implements the 'signIn' function.Expected behavior
The app should function properly and the
reqobject should contain aqueryprop, or the NextAuth documentation should be updated to include whatever migration steps must be made.