Replies: 3 comments 1 reply
-
I would like this too |
Beta Was this translation helpful? Give feedback.
-
Hello, We will use the middleware, so every request that matches it will run the function to check whether the auth is provided or not. The first thing we need to do is add import { NextRequest, NextResponse } from "next/server";
export const config = {
matcher: [
'/private/(.*)',
'/_next/static/chunks/pages/private/(.*)'
],
}
export function middleware(req: NextRequest) {
const basicAuth = req.headers.get("authorization");
const url = req.nextUrl;
if (basicAuth) {
const authValue = basicAuth.split(" ")[1];
const [user, pwd] = atob(authValue).split(":");
if (user === "user" && pwd === "password") {
return NextResponse.next();
}
}
url.pathname = "/api/auth";
return NextResponse.rewrite(url);
} That code will redirect to the endpoint import type { NextApiRequest, NextApiResponse } from "next";
export default function handler(_: NextApiRequest, res: NextApiResponse) {
res.setHeader("WWW-authenticate", 'Basic realm="Secure Area"');
res.statusCode = 401;
res.end(`Auth Required.`);
} |
Beta Was this translation helpful? Give feedback.
-
is there any logout method |
Beta Was this translation helpful? Give feedback.
-
For example, limited document disclosure for logged-in users only.
Regarding private documents, search keywords are also excluded from unlogged-in users.
Beta Was this translation helpful? Give feedback.
All reactions