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

middleware works without auth #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DB_USERNAME=''
DB_PASSWORD='pscale_pw_'

# @see https://next-auth.js.org/configuration/options#nextauth_url
AUTH_URL='http://localhost:3000'
AUTH_URL='http://local.run:3000'
AUTH_REDIRECT_PROXY_URL="http://localhost:3001/api"

# You can generate the secret via 'openssl rand -base64 32' on Unix
Expand Down
23 changes: 5 additions & 18 deletions apps/nextjs/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,15 @@ export const config = {
],
};

// export const config = {
// matcher: [
// /*
// * The matcher applies the middleware to specific paths based on the following criteria:
// * 1. Exclude paths with a file extension (e.g., .jpg, .css, .js). This is done using the regex
// * pattern that filters out paths ending in a dot followed by word characters.
// * 2. Exclude paths starting with /_next/, which are used internally by Next.js.
// * 3. Include the root path ("/") explicitly.
// * 4. Include paths starting with /api or /trpc, followed by any characters. This allows
// * the middleware to be applied to all API routes and any routes starting with /trpc.
// */
// "/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"
// ],
// };

export default auth((req) => {
const url = req.nextUrl;
const isLoggedIn = !!req.auth;

// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
let hostname = req.headers
.get("host")!
.replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);
.replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`)
.replace(".local.run:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);

// special case for Vercel preview deployment URLs
if (
Expand Down Expand Up @@ -74,13 +60,14 @@ export default auth((req) => {
// Handle main domain
if (
hostname === "localhost:3000" ||
hostname.endsWith(`.${env.NEXT_PUBLIC_ROOT_DOMAIN}`) // e.g. www.nourish.run
hostname === "local.run:3000" ||
hostname.endsWith(`.${env.NEXT_PUBLIC_ROOT_DOMAIN}`) // e.g. www.domain.com
) {
console.log(isLoggedIn, hostname, req.url, "mainnnnnnn");

const isDevelopment = process.env.NODE_ENV === "development";
const baseUrl = isDevelopment
? "http://app.localhost:3000"
? `http://app.${hostname}`
: `https://app.${env.NEXT_PUBLIC_ROOT_DOMAIN}`;

if (isLoggedIn && authRoutes.includes(path)) {
Expand Down
77 changes: 77 additions & 0 deletions apps/nextjs/src/middlewaress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";

import { env } from "~/env";

export const config = {
matcher: [
/*
* Match all paths except for:
* 1. /api/ routes
* 2. /_next/ (Next.js internals)
* 3. /_proxy/ (special page for OG tags proxying)
* 4. /_static (inside /public)
* 5. /_vercel (Vercel internals)
* 6. Static files (e.g. /favicon.ico, /sitemap.xml, /robots.txt, etc.)
*/
"/((?!api/|_next/|_proxy/|_static|_vercel|[\\w-]+\\.\\w+).*)",
],
};

export default async function middleware(req: NextRequest) {
const url = req.nextUrl;

// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
let hostname = req.headers
.get("host")!
.replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`)
.replace(".local.run:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);

// special case for Vercel preview deployment URLs
if (
hostname.includes("---") &&
hostname.endsWith(`.${process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_SUFFIX}`)
) {
hostname = `${hostname.split("---")[0]}.${env.NEXT_PUBLIC_ROOT_DOMAIN}`;
}

const searchParams = req.nextUrl.searchParams.toString();
// Get the pathname of the request (e.g. /, /about, /blog/first-post)
const path = `${url.pathname}${
searchParams.length > 0 ? `?${searchParams}` : ""
}`;

console.log(hostname, path, url, req.url);

// Handle app subdomain
if (hostname == `app.${env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
console.log(hostname, req.url, "apppppppppp");
// if (!isLoggedIn) {
// return NextResponse.redirect(new URL("/signin", req.url));
// }
return NextResponse.rewrite(
new URL(`/app${path === "/" ? "" : path}`, req.url),
);
}

// Handle main domain
if (
hostname === "localhost:3000" ||
hostname === "local.run:3000" ||
hostname.endsWith(`.${env.NEXT_PUBLIC_ROOT_DOMAIN}`) // e.g. www.domain.com
) {
console.log(hostname, req.url, "mainnnnnnn");

const isDevelopment = process.env.NODE_ENV === "development";
const baseUrl = isDevelopment
? `http://app.${hostname}`
: `https://app.${env.NEXT_PUBLIC_ROOT_DOMAIN}`;

return NextResponse.rewrite(
new URL(`/home${path === "/" ? "" : path}`, req.url),
);
}

// Allow normal processing for all other requests
return NextResponse.next();
}
Loading