Connect, collaborate, and grow with a community of TypeScript developers
adds basic auth
Chris Bautista9478f0ad73de
diff --git a/src/app/api/lol/route.ts b/src/app/api/lol/route.tsnew file mode 100644index 00000000..ab695c4e--- /dev/null+++ b/src/app/api/lol/route.ts@@ -0,0 +1,6 @@+export async function GET() {+ return new Response('Auth required', {+ status: 401,+ headers: { 'WWW-authenticate': 'Basic realm="Secure Area"' },+ });+}diff --git a/src/middleware.ts b/src/middleware.tsindex e70aafa1..1c6b1e8b 100644--- a/src/middleware.ts+++ b/src/middleware.ts@@ -1,43 +1,22 @@-import { getToken } from 'next-auth/jwt';-import { withAuth } from 'next-auth/middleware';-import { NextResponse } from 'next/server';+import { NextRequest, NextResponse } from 'next/server';-export default withAuth(- async function middleware(req) {- const token = await getToken({ req });- const isAuth = !!token;- const isAuthPage =- req.nextUrl.pathname.startsWith('/login') || req.nextUrl.pathname.startsWith('/register');+export function middleware(req: NextRequest) {+ if (process.env.NODE_ENV === 'production') {+ const basicAuth = req.headers.get('authorization');+ const url = req.nextUrl;- if (isAuthPage) {- if (isAuth) {- return NextResponse.redirect(new URL('/', req.url));- }-- return null;- }+ if (basicAuth) {+ const authValue = basicAuth.split(' ')[1];+ const [user, pwd] = atob(authValue ?? '').split(':');- if (!isAuth) {- let from = req.nextUrl.pathname;- if (req.nextUrl.search) {- from += req.nextUrl.search;+ if (user === 'trash' && pwd === 'isg0d') {+ return NextResponse.next();}-- return NextResponse.redirect(new URL(`/login?from=${encodeURIComponent(from)}`, req.url));}- },- {- callbacks: {- authorized() {- // This is a work-around for handling redirect on auth pages.- // We return true here so that the middleware function above- // is always called.- return true;- },- },- },-);+ url.pathname = '/api/lol';++ return NextResponse.rewrite(url);+ }-export const config = {- matcher: ['/login', '/register'],-};+ return NextResponse.next();+}