Chandra
Chandra
TTCTheo's Typesafe Cult
Created by Chandra on 6/29/2024 in #questions
Nextjs middleware error while rewrite the route
export default auth((req: NextAuthRequest) => {
const url = req.nextUrl;
const isLoggedIn = !!req.auth;

const isAuthRoute = authRoutes.includes(url.pathname);

// 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}`);

hostname = hostname.replace('www.', ''); // remove www. from 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}` : ''
}`;

if (hostname === `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
if (!isLoggedIn && !isAuthRoute) {
return NextResponse.redirect(new URL('/login', req.url));
} else if (isLoggedIn && isAuthRoute) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, req.url));
}

// Continue to the next middleware or serve the root content
return NextResponse.rewrite(
new URL(`/app${path === '/' ? '' : path}`, req.url),
);
}
// rewrite root application to `/home` folder
if (
hostname === 'localhost:3000' ||
hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
) {
return NextResponse.rewrite(
new URL(`/home${path === '/' ? '' : path}`, req.url),
);
}

// rewrite everything else to `/[domain]/[path] dynamic route
return NextResponse.rewrite(new URL(`/${hostname}${path}`, req.url));
});

// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};
export default auth((req: NextAuthRequest) => {
const url = req.nextUrl;
const isLoggedIn = !!req.auth;

const isAuthRoute = authRoutes.includes(url.pathname);

// 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}`);

hostname = hostname.replace('www.', ''); // remove www. from 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}` : ''
}`;

if (hostname === `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
if (!isLoggedIn && !isAuthRoute) {
return NextResponse.redirect(new URL('/login', req.url));
} else if (isLoggedIn && isAuthRoute) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, req.url));
}

// Continue to the next middleware or serve the root content
return NextResponse.rewrite(
new URL(`/app${path === '/' ? '' : path}`, req.url),
);
}
// rewrite root application to `/home` folder
if (
hostname === 'localhost:3000' ||
hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
) {
return NextResponse.rewrite(
new URL(`/home${path === '/' ? '' : path}`, req.url),
);
}

// rewrite everything else to `/[domain]/[path] dynamic route
return NextResponse.rewrite(new URL(`/${hostname}${path}`, req.url));
});

// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};
4 replies
TTCTheo's Typesafe Cult
Created by Chandra on 6/29/2024 in #questions
Nextjs middleware error while rewrite the route
import { type NextRequest, NextResponse } from 'next/server';

import { DEFAULT_LOGIN_REDIRECT, authRoutes } from '@/routes';
import authConfig from '@drafton/auth/config';
import NextAuth from 'next-auth';
import type { Session } from 'next-auth/types';

const { auth } = NextAuth(authConfig);

interface NextAuthRequest extends NextRequest {
auth: Session | null;
}
import { type NextRequest, NextResponse } from 'next/server';

import { DEFAULT_LOGIN_REDIRECT, authRoutes } from '@/routes';
import authConfig from '@drafton/auth/config';
import NextAuth from 'next-auth';
import type { Session } from 'next-auth/types';

const { auth } = NextAuth(authConfig);

interface NextAuthRequest extends NextRequest {
auth: Session | null;
}
4 replies
TTCTheo's Typesafe Cult
Created by Chandra on 6/6/2024 in #questions
How can we implement the /user/month plan for team.
If plan is yearly then, next payment will be next year so in that time frame user can remove team member and they can save money. What is potential solution to that
6 replies
TTCTheo's Typesafe Cult
Created by Chandra on 6/6/2024 in #questions
How can we implement the /user/month plan for team.
No description
6 replies