Skip to content
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

Not working with latest next 13 #35

Open
suil opened this issue Jun 22, 2023 · 5 comments
Open

Not working with latest next 13 #35

suil opened this issue Jun 22, 2023 · 5 comments

Comments

@suil
Copy link

suil commented Jun 22, 2023

Somehow it's not working with latest project structure that are generated by npx create-next-app@latest

@echeng8
Copy link

echeng8 commented Jul 9, 2023

@suil works for me, what's your middleware.ts like? I just copied the one from examples.

@phsantiago
Copy link

phsantiago commented Jul 26, 2023

Error: The Edge Function "middleware" is referencing unsupported modules:
22:55:26.396 | - next: stream, fs, url, path

// middleware.js
import { createNextAuthMiddleware } from 'nextjs-basic-auth-middleware'

export const middleware = createNextAuthMiddleware(options)

export const config = {
    matcher: ['/(.*)'], // Replace this with your own matcher logic
}

"nextjs-basic-auth-middleware": "^3.1.0",

@myrkytyn
Copy link

@phsantiago
Did you declare options?
What version of Next.JS you are using?

const options = {
  users: [{ name: "username", password: "password" }],
};

@phsantiago
Copy link

@myrkytyn sorry, I deleted the repo, so I'm not sure about the version, I think it was 13.4 because was a brand new installation.
I did not declare options.

I could make it work but with a different implementation in another repo:

"next": "12.2.12",

//middleware.js
import { NextResponse } from "next/server";

import checkBasicAuth from "@utils/middleware/checkBasicAuth";

export function middleware(request) {
  if (
    !checkBasicAuth(request.headers.get("authorization"), {
      username: process.env.AUTH_USERNAME,
      password: process.env.AUTH_PASSWORD,
    })
  ) {
    return NextResponse.rewrite(
      `${request.nextUrl.protocol}//${request.nextUrl.host}/401`,
      {
        status: 401,
        headers: {
          "WWW-Authenticate": 'Basic realm="Secure Area"',
        },
      },
    );
  }

  return NextResponse.next();
}

@feenn
Copy link

feenn commented Oct 26, 2023

The middleware.ts/js of NextJS 13 must be placed in the root directory, and it will not work if placed under src.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants