Skip to content

Commit

Permalink
feat: fix login for any url
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevo David committed Sep 23, 2024
1 parent 25aa3d5 commit ec0259d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2,923 deletions.
17 changes: 6 additions & 11 deletions apps/backend/src/api/routes/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { removeSubdomain } from '@gitroom/helpers/subdomain/subdomain.management';
import { getCookieUrlFromDomain } from '@gitroom/helpers/subdomain/subdomain.management';
import { pricing } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/pricing';
import { ApiTags } from '@nestjs/swagger';
import { UsersService } from '@gitroom/nestjs-libraries/database/prisma/users/users.service';
Expand Down Expand Up @@ -92,8 +92,7 @@ export class UsersController {
}

response.cookie('impersonate', id, {
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
secure: true,
httpOnly: true,
sameSite: 'none',
Expand Down Expand Up @@ -163,8 +162,7 @@ export class UsersController {
@Res({ passthrough: true }) response: Response
) {
response.cookie('showorg', id, {
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
secure: true,
httpOnly: true,
sameSite: 'none',
Expand All @@ -177,8 +175,7 @@ export class UsersController {
@Post('/logout')
logout(@Res({ passthrough: true }) response: Response) {
response.cookie('auth', '', {
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
secure: true,
httpOnly: true,
maxAge: -1,
Expand All @@ -187,8 +184,7 @@ export class UsersController {
});

response.cookie('showorg', '', {
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
secure: true,
httpOnly: true,
maxAge: -1,
Expand All @@ -197,8 +193,7 @@ export class UsersController {
});

response.cookie('impersonate', '', {
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
secure: true,
httpOnly: true,
maxAge: -1,
Expand Down
33 changes: 20 additions & 13 deletions apps/frontend/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +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';
import { getCookieUrlFromDomain } from '@gitroom/helpers/subdomain/subdomain.management';

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
Expand All @@ -19,8 +19,7 @@ export async function middleware(request: NextRequest) {
httpOnly: true,
secure: true,
maxAge: -1,
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
});
return response;
}
Expand All @@ -30,9 +29,17 @@ export async function middleware(request: NextRequest) {

if (nextUrl.href.indexOf('/auth') === -1 && !authCookie) {
const providers = ['google', 'settings'];
const findIndex = providers.find(p => nextUrl.href.indexOf(p) > -1);
const additional = !findIndex ? '' : (url.indexOf('?') > -1 ? '&' : '?') + `provider=${(findIndex === 'settings' ? 'github' : findIndex).toUpperCase()}`;
return NextResponse.redirect(new URL(`/auth${url}${additional}`, nextUrl.href));
const findIndex = providers.find((p) => nextUrl.href.indexOf(p) > -1);
const additional = !findIndex
? ''
: (url.indexOf('?') > -1 ? '&' : '?') +
`provider=${(findIndex === 'settings'
? 'github'
: findIndex
).toUpperCase()}`;
return NextResponse.redirect(
new URL(`/auth${url}${additional}`, nextUrl.href)
);
}

// If the url is /auth and the cookie exists, redirect to /
Expand All @@ -49,8 +56,7 @@ export async function middleware(request: NextRequest) {
httpOnly: true,
secure: true,
expires: new Date(Date.now() + 15 * 60 * 1000),
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
});
return redirect;
}
Expand Down Expand Up @@ -81,8 +87,7 @@ export async function middleware(request: NextRequest) {
httpOnly: true,
secure: true,
expires: new Date(Date.now() + 15 * 60 * 1000),
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
});
}

Expand All @@ -91,7 +96,10 @@ export async function middleware(request: NextRequest) {

if (nextUrl.pathname === '/') {
return NextResponse.redirect(
new URL(!!process.env.IS_GENERAL ? '/launches' : `/analytics`, nextUrl.href)
new URL(
!!process.env.IS_GENERAL ? '/launches' : `/analytics`,
nextUrl.href
)
);
}

Expand All @@ -109,8 +117,7 @@ export async function middleware(request: NextRequest) {
httpOnly: true,
secure: true,
expires: new Date(Date.now() + 15 * 60 * 1000),
domain:
'.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!),
});
}

Expand Down
37 changes: 3 additions & 34 deletions libraries/helpers/src/subdomain/subdomain.management.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
import {allTwoLevelSubdomain} from "./all.two.level.subdomain";
const ipRegex = /^(https?:\/\/)?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+)?$/;

export function removeSubdomain(domain: string) {
// Check if the domain is an IP address with optional port
if (ipRegex.test(domain)) {
return domain; // Return the original domain if it's an IP address
}
// 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('.');
}

import { parse } from 'tldts';

export function getCookieUrlFromDomain(domain: string) {
const url = removeSubdomain(domain);
const urlObj = new URL(url);
if (!ipRegex.test(domain)) {
return '.' + urlObj.hostname
}

return urlObj.hostname;
const url = parse(domain);
return url.domain! ? "." + url.domain! : url.hostname!;
}
Loading

0 comments on commit ec0259d

Please sign in to comment.