Next.js Discord

Discord Forum

"Middleware" in app directory

Answered
Gull-billed Tern posted this in #help-forum
Open in Discord
Gull-billed TernOP
I have this code block:

export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const projectId = params.id;

// the "getUserId" is a function in a separate file
const userId = await getUserId();
if (!userId) {
return NextResponse.json(
{ message: "Unauthorized request." },
{ status: 401 }
);
}

//rest of the code


I need the user to be authenticated to access all endpoints (except login and other auths), is there a way I can create sort of a middleware thing so I don't have to check for userId on every endpoint manually?
Answered by Giant panda
Did you try to search "middleware" in the docs?
View full answer

2 Replies

Giant panda
Did you try to search "middleware" in the docs?
Answer
Gull-billed TernOP
Errmmm no, my bad let me check