Next.js Discord

Discord Forum

Auth middleware error: edgeFunction is not a function

Unanswered
Swedish Lapphund posted this in #help-forum
Open in Discord
Swedish LapphundOP
I am trying to implement protected routes in my application through middleware. Take a look at the code:

@/lib/auth.js:
import { cookies } from 'next/headers'
const apiUrl = process.env.NEXT_PUBLIC_API_URL;

async function returnAuthStatus() {
    const cookieStore = cookies()
    try {
        const response = await fetch(`${apiUrl}/auth/check-auth`, {
            method: 'GET',
            headers: {
                'Cookie': `${cookieStore.get('user_sid').name}=${cookieStore.get('user_sid').value}`
            }
        });

        const json = await response.json();
        if(json.message === "User is authenticated") {
            return true;
        } else {
            return false;
        }
    } catch (error) {
        console.log(error.message);
    }
}

export const isAuthenticated = await returnAuthStatus();

TLDR ^ it outputs the user's auth status to true or false

This is being used in root/middleware.js:
import { NextResponse } from 'next/server'
import { isAuthenticated } from '@/lib/auth'
 
// This function can be marked `async` if using `await` inside
export function middleware(request) {
  return NextResponse.redirect(new URL('/', request.url))
}
 
// See "Matching Paths" below to learn more
export const config = {
  matcher: '/dashboard',
}


The problem:

When I visit /dashboard I get the error TypeError: edgeFunction is not a function

If I just remove the isAuthenticated import from middleware, the error disappears. Is this a bug?

0 Replies