-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type "AppRouteHandlerFnContext" is not a valid type for the function's second argument. #12224
Comments
Same error here. Cannot build my project |
I've just come across this error too. It seems to already be fixed on |
Ok nice. So we need to wait for the next release. Thx |
Just got hit by this as well! Any notes on the next release? |
Sure, it's just a temporary fix, but it lets me move forward with building my app. export const POST = auth(async (request) => {
try {
const {
code
} = await request.json();
const decodedData = decrypt(code);
const isValid = new Date(decodedData.expiresAt).getTime() > new Date().getTime() ;
if (!isValid) {
return Response.json({ isValid });
}
return Response.json({ isValid, decodedData });
} catch (error: any) {
return Response.json({ message: error.message}, { status: 500 });
}
}) as any; <------ SOLUTION HERE |
Why does this take so long to release? |
What is the status on releasing this particular fix? The fix has been made >6 weeks ago, but apparently not released yet I cannot make a production build unless I temporarily add:
to my next.config.mjs ===
|
The same problem, just copy and paste the example code: import { auth } from '@/auth';
import { NextResponse } from 'next/server';
export const GET = auth((req) => {
if (req.auth) {
return NextResponse.json({ data: 'Protected data' });
}
return NextResponse.json({ message: 'Not authenticated' }, { status: 401 });
}); Hopefully a new version is released soon 😭 |
I'm on next export async function GET(request: NextRequest) {
const authSession = await auth();
if (!authSession) {
return NextResponse.json(
{
message: 'Unauthorized',
medias: [],
},
{ status: 401 },
);
}
} This is my Front End request: //import { cookies } from 'next/headers';
//const cookiesSession = await cookies();
const response = await axios.get(`/api/media?page=${page}&limit=${limit}`, {
headers: {
'Content-Type': 'application/json',
//Cookie: cookiesSession.toString(), //the cookies are required if using SSR
},
withCredentials: true,
}); |
@larodiel, how did you figure that one out? Also, I hope you are right! I prefer explicit call and check the returned value instead of that closure way to do things |
@yordis I was reading the documentation https://authjs.dev/getting-started/session-management/protecting and as call the auth function was nothing passing on the deploy step, I tried to validate it as we validate pages, and it worked. |
This is what AI (claude) is also suggesting. Poking around here and glad to see others running with the same method. I'll probably make a simple util function so I don't have to re-type this everywhere though. |
wrapping is less ergonomic anyway tbh |
This is just a reimplementation of what I think import { auth } from '@/auth'
import { NextRequest } from 'next/server'
export interface NextRequestExt extends NextRequest {
// this is for my needs
auth?: { role?: string; user?: { id?: string; email?: string | null } }
}
export const withAuth = (
handler: (req: NextRequestExt) => Promise<Response>
) => {
return async function (req: NextRequestExt) {
try {
const session = await auth()
if (!session) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
// Add session to request for use in handler
req.auth = session
return handler(req)
} catch (error) {
console.error(error)
return Response.json({ error: 'Internal server error' }, { status: 500 })
}
}
} import { NextResponse } from 'next/server'
import { withAuth } from '@/utils/auth'
import type { NextRequestExt } from '@/utils/auth'
export const GET = withAuth(async function GET(req: NextRequestExt) {
try {
return NextResponse.json(req.auth)
} catch (error) {
console.error(error)
return NextResponse.json(
{ error: 'Failed to test' },
{ status: 500 }
)
}
}) I'm also using the for RBAC. export const withRole = (
role: string,
handler: (req: NextRequestExt) => Promise<Response>
) => {
return withAuth(async (req: NextRequestExt) => {
try {
// db request for user role based on unique email values
const userRole = await getRole(req.auth?.user?.email ?? '')
if (userRole !== role) {
return Response.json({ error: 'Forbidden' }, { status: 403 })
}
req.auth!.role = userRole
return handler(req)
} catch (error) {
console.error(error)
return Response.json({ error: 'Internal server error' }, { status: 500 })
}
})
} Edit: I changed those to use |
Why the new release is delayed? Couldn't we push a hotfix version? |
Environment
Reproduction URL
https://github.com/KennyMwendwaX/task-trace/
Describe the issue
invitation code api route handler:
This is the api route with the issue, it works perfectly in development when you build using the npm run build, the following error is produced on the terminal:
How to reproduce
Expected behavior
It actually works quite good in development but when I build the project it fails.
The text was updated successfully, but these errors were encountered: