feat: remove subdomain

This commit is contained in:
Nevo David 2024-03-09 19:47:57 +07:00
parent d9faf4b8f5
commit fdf88b73f5
5 changed files with 1435 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import { LoginUserDto } from '@gitroom/nestjs-libraries/dtos/auth/login.user.dto
import { AuthService } from '@gitroom/backend/services/auth/auth.service';
import { ForgotReturnPasswordDto } from '@gitroom/nestjs-libraries/dtos/auth/forgot-return.password.dto';
import { ForgotPasswordDto } from '@gitroom/nestjs-libraries/dtos/auth/forgot.password.dto';
import {removeSubdomain} from "@gitroom/helpers/subdomain/subdomain.management";
@Controller('/auth')
export class AuthController {
@ -28,7 +29,7 @@ export class AuthController {
);
response.cookie('auth', jwt, {
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
secure: true,
httpOnly: true,
sameSite: 'none',
@ -37,7 +38,7 @@ export class AuthController {
if (typeof addedOrg !== 'boolean' && addedOrg?.organizationId) {
response.cookie('showorg', addedOrg.organizationId, {
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
secure: true,
httpOnly: true,
sameSite: 'none',
@ -72,7 +73,7 @@ export class AuthController {
);
response.cookie('auth', jwt, {
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
secure: true,
httpOnly: true,
sameSite: 'none',
@ -81,7 +82,7 @@ export class AuthController {
if (typeof addedOrg !== 'boolean' && addedOrg?.organizationId) {
response.cookie('showorg', addedOrg.organizationId, {
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
secure: true,
httpOnly: true,
sameSite: 'none',

View File

@ -20,6 +20,7 @@ import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import {removeSubdomain} from "@gitroom/helpers/subdomain/subdomain.management";
@Controller('/user')
export class UsersController {
@ -100,7 +101,7 @@ export class UsersController {
@Res({ passthrough: true }) response: Response
) {
response.cookie('showorg', id, {
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
secure: true,
httpOnly: true,
sameSite: 'none',

View File

@ -1,6 +1,7 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { fetchBackend } from '@gitroom/helpers/utils/custom.fetch.func';
import {removeSubdomain} from "@gitroom/helpers/subdomain/subdomain.management";
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
@ -19,7 +20,7 @@ export async function middleware(request: NextRequest) {
httpOnly: true,
secure: true,
maxAge: -1,
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
});
return response;
}
@ -45,7 +46,7 @@ export async function middleware(request: NextRequest) {
httpOnly: true,
secure: true,
expires: new Date(Date.now() + 15 * 60 * 1000),
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
});
return redirect;
}
@ -76,7 +77,7 @@ export async function middleware(request: NextRequest) {
httpOnly: true,
secure: true,
expires: new Date(Date.now() + 15 * 60 * 1000),
domain: '.' + new URL(process.env.FRONTEND_URL!).hostname,
domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
});
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,21 @@
import {allTwoLevelSubdomain} from "./all.two.level.subdomain";
export function removeSubdomain(domain: string) {
// Split the domain into its parts
const parts = domain.split('.');
// Check if there are at least two parts (e.g., 'example.com')
if (parts.length < 2) {
return domain; // Return the original domain if it's too short to have a subdomain
}
if (parts.length > 2) {
const lastTwo = parts.slice(-2).join('.');
if (allTwoLevelSubdomain.includes(lastTwo)) {
return 'https://' + parts.slice(-3).join('.'); // Return the last three parts for known second-level domains
}
}
// Return the last two parts for standard domains
return 'https://' + parts.slice(-2).join('.');
}