Skip to content
Merged
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
34 changes: 16 additions & 18 deletions src/app/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ import {
PublicTenantAuthConfig,
} from "@/lib/types";
import { getApiErrorMessage, isNotAllowedError } from "@/lib/errors";
import {
PLATFORM_TENANT_ID,
PublicOAuthProvider,
SOCIAL_PROVIDER_META,
} from "@/lib/social-login";
import { getPlatformTenantId, PublicOAuthProvider, SOCIAL_PROVIDER_META } from "@/lib/social-login";
import { getPublicEnv } from "@/lib/public-env";
import { isWebAuthnSupported } from "@/lib/webauthn-support";
import { LoginFormSkeleton } from "@/components/auth/login-form-skeleton";

Expand Down Expand Up @@ -243,16 +240,15 @@ function LoginPageContent() {
}, [enrollData]);

const { data: authConfig, isLoading: isLoadingAuthConfig } = useQuery<PublicTenantAuthConfig>({
queryKey: ["publicAuthConfig", PLATFORM_TENANT_ID],
queryKey: ["publicAuthConfig"],
queryFn: async () => {
const { data } = await apiClient.get<PublicTenantAuthConfig>("/auth/auth-config", {
params: { tenant_id: PLATFORM_TENANT_ID },
});
const { data } = await apiClient.get<PublicTenantAuthConfig>("/auth/auth-config");
return data;
},
enabled: !!PLATFORM_TENANT_ID,
});

const platformTenantId = authConfig?.tenant_id ?? getPlatformTenantId();

const allowedMethods = new Set(authConfig?.allowed_methods ?? []);
const showEmailPassword = allowedMethods.has("email_password");
const showMagicLink = allowedMethods.has("magic_link");
Expand All @@ -265,15 +261,17 @@ function LoginPageContent() {
const { data: socialProviders = [], isLoading: isLoadingSocialProviders } = useQuery<
PublicOAuthProvider[]
>({
queryKey: ["loginSocialProviders", PLATFORM_TENANT_ID],
queryKey: ["loginSocialProviders", platformTenantId],
queryFn: async () => {
const { data } = await apiClient.get<PublicOAuthProvider[]>(
"/auth/oauth/providers",
{ params: { tenant_id: PLATFORM_TENANT_ID } }
platformTenantId
? { params: { tenant_id: platformTenantId } }
: undefined
);
return data;
},
enabled: !!PLATFORM_TENANT_ID && showSocial,
enabled: !!platformTenantId && showSocial,
});

const passwordForm = useForm<z.infer<typeof loginSchema>>({
Expand Down Expand Up @@ -315,15 +313,15 @@ function LoginPageContent() {
const sendMagicLink = async (email: string) => {
await apiClient.post("/auth/magic-link/request", {
email,
tenant_id: PLATFORM_TENANT_ID || undefined,
tenant_id: platformTenantId || undefined,
});
};

const loginMutation = useMutation({
mutationFn: async (values: { email: string; password: string }) => {
const { data } = await apiClient.post("/auth/login", {
...values,
tenant_id: PLATFORM_TENANT_ID || undefined,
tenant_id: platformTenantId || undefined,
});
return data;
},
Expand Down Expand Up @@ -446,7 +444,7 @@ function LoginPageContent() {

try {
const { data } = await apiClient.post("/auth/webauthn/authenticate/begin", {
tenant_id: PLATFORM_TENANT_ID || undefined,
tenant_id: platformTenantId || undefined,
});

const assertion = await startAuthentication({ optionsJSON: data.options });
Expand All @@ -465,7 +463,7 @@ function LoginPageContent() {
userHandle: assertion.response.userHandle,
},
},
tenant_id: PLATFORM_TENANT_ID || undefined,
tenant_id: platformTenantId || undefined,
}
);

Expand All @@ -482,7 +480,7 @@ function LoginPageContent() {
};

const handleSocialLogin = (provider: string, tenantId: string) => {
const baseUrl = process.env.NEXT_PUBLIC_API_URL;
const baseUrl = getPublicEnv().API_URL;
const params = new URLSearchParams({ tenant_id: tenantId });
window.location.href = `${baseUrl}/auth/oauth/${provider}/login?${params.toString()}`;
};
Expand Down
4 changes: 4 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import { ThemeProvider } from "@/components/theme-provider";
import Providers from "@/components/providers";
import { RuntimeEnv } from "@/components/runtime-env";
import { Toaster } from "@/components/ui/sonner";

const geistSans = Geist({
Expand All @@ -27,6 +28,9 @@ export default function RootLayout({
}>) {
return (
<html lang="en" suppressHydrationWarning>
<head>
<RuntimeEnv />
</head>
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
Expand Down
4 changes: 2 additions & 2 deletions src/components/contact-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { getPublicEnv } from "@/lib/public-env";

const COMPANY_SIZES = ["1–10", "11–50", "51–200", "201–1,000", "1,000+"];
const MAU_RANGES = [
Expand Down Expand Up @@ -63,8 +64,7 @@ export function ContactForm() {
return;
}

const apiBase =
process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000/api/v1";
const apiBase = getPublicEnv().API_URL || "http://localhost:8000/api/v1";

setIsSubmitting(true);
try {
Expand Down
15 changes: 15 additions & 0 deletions src/components/runtime-env.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function RuntimeEnv() {
const config = {
API_URL: process.env.NEXT_PUBLIC_API_URL ?? "",
APP_URL: process.env.NEXT_PUBLIC_APP_URL ?? "",
PLATFORM_TENANT_ID: process.env.NEXT_PUBLIC_PLATFORM_TENANT_ID ?? "",
};

return (
<script
dangerouslySetInnerHTML={{
__html: `window.__PUBLIC_ENV__=${JSON.stringify(config)}`,
}}
/>
);
}
11 changes: 7 additions & 4 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import axios from "axios";
import { useAuthStore } from "@/stores/auth-store";

const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "";
import { getPublicEnv } from "@/lib/public-env";

export const apiClient = axios.create({
baseURL: API_BASE_URL,
baseURL: "",
headers: {
"Content-Type": "application/json",
},
Expand All @@ -13,6 +12,10 @@ export const apiClient = axios.create({
// Request Interceptor to add Access Token and Active Tenant
apiClient.interceptors.request.use(
(config) => {
if (!config.baseURL) {
config.baseURL = getPublicEnv().API_URL;
}

const state = useAuthStore.getState();
const token = state.accessToken;
const tenantId = state.activeTenantId;
Expand Down Expand Up @@ -47,7 +50,7 @@ apiClient.interceptors.response.use(
if (refreshToken) {
try {
// Note: using raw axios to avoid interceptor infinite loops
const { data } = await axios.post(`${API_BASE_URL}/auth/refresh`, {
const { data } = await axios.post(`${getPublicEnv().API_URL}/auth/refresh`, {
refresh_token: refreshToken,
});

Expand Down
31 changes: 31 additions & 0 deletions src/lib/public-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
declare global {
interface Window {
__PUBLIC_ENV__?: {
API_URL: string;
APP_URL: string;
PLATFORM_TENANT_ID: string;
};
}
}

export type PublicEnv = {
API_URL: string;
APP_URL: string;
PLATFORM_TENANT_ID: string;
};

export function getPublicEnv(): PublicEnv {
if (typeof window !== "undefined" && window.__PUBLIC_ENV__) {
return window.__PUBLIC_ENV__;
}

return {
API_URL: process.env.NEXT_PUBLIC_API_URL || "",
APP_URL: process.env.NEXT_PUBLIC_APP_URL || "",
PLATFORM_TENANT_ID: process.env.NEXT_PUBLIC_PLATFORM_TENANT_ID || "",
};
}

export function getPlatformTenantId(): string {
return getPublicEnv().PLATFORM_TENANT_ID;
}
2 changes: 1 addition & 1 deletion src/lib/social-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface PublicOAuthProvider {
tenant_id: string;
}

export const PLATFORM_TENANT_ID = process.env.NEXT_PUBLIC_PLATFORM_TENANT_ID || "";
export { getPlatformTenantId } from "@/lib/public-env";

export const SOCIAL_PROVIDER_META: Record<
string,
Expand Down
Loading