i am trying to make an auth in nextjs. and in the root middleware.ts i have this:
import { auth } from "./auth";
export default auth((req) => {
const isLoggedIn = !!req.auth;
console.log("Is Logged In: ", isLoggedIn);
});
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
// Always run for API routes
'/(api|trpc)(.*)',
],
};
i have been following this /getting-started/installation?framework=next-js and using latest nextjs.
now the problem is when i just refresh the /auth/login
route. i am expecting this:
console.log("Is Logged In: ", isLoggedIn);
to make log on the server terminal but its not, its just
✓ Ready in 3.2s
○ Compiling /auth/register ...
✓ Compiled /auth/register in 3.3s (735 modules)
✓ Compiled in 970ms (340 modules)
GET /auth/register 200 in 4384ms
○ Compiling /auth/login ...
✓ Compiled /auth/login in 760ms (733 modules)
GET /auth/login 200 in 885ms
GET /auth/register 200 in 32ms
do you have any idea?
i am new to nextjs, so this is a little challenge
i am trying to make an auth in nextjs. and in the root middleware.ts i have this:
import { auth } from "./auth";
export default auth((req) => {
const isLoggedIn = !!req.auth;
console.log("Is Logged In: ", isLoggedIn);
});
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
// Always run for API routes
'/(api|trpc)(.*)',
],
};
i have been following this https://authjs.dev/getting-started/installation?framework=next-js and using latest nextjs.
now the problem is when i just refresh the /auth/login
route. i am expecting this:
console.log("Is Logged In: ", isLoggedIn);
to make log on the server terminal but its not, its just
✓ Ready in 3.2s
○ Compiling /auth/register ...
✓ Compiled /auth/register in 3.3s (735 modules)
✓ Compiled in 970ms (340 modules)
GET /auth/register 200 in 4384ms
○ Compiling /auth/login ...
✓ Compiled /auth/login in 760ms (733 modules)
GET /auth/login 200 in 885ms
GET /auth/register 200 in 32ms
do you have any idea?
i am new to nextjs, so this is a little challenge
Share Improve this question asked Feb 5 at 16:30 FilFil 8,87317 gold badges63 silver badges89 bronze badges 01 Answer
Reset to default 0You need to export a function called middleware
, see the example in the documentation:
https://nextjs.org/docs/app/building-your-application/routing/middleware#convention
You are exporting the result of the call to auth(...)
as the default export, which will not be recognized as a middleware by next.js.
In the documentation you provided, they do it like this:
export { auth as middleware } from "@/auth"
Which will export auth
with the name middleware
Then you can't do your logging of course.
If I understand the signature of auth
correctly, you should be able to do something like this to get your logging working:
import { auth } from "./auth";
export function middleware(request) {
return auth((req) => {
const isLoggedIn = !!req.auth;
console.log("Is Logged In: ", isLoggedIn);
})(request);
}
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
// Always run for API routes
'/(api|trpc)(.*)',
],
};