Skip to content

Commit a07f3a8

Browse files
committed
feat: dynamic api_base_url
1 parent 2b13029 commit a07f3a8

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

cli/src/cluster/command/dashboard.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use std::path::PathBuf;
55

66
pub fn command(name: Option<String>) -> anyhow::Result<()> {
77
let cluster = Cli::get_default_cluster_or_ask(name)?;
8-
let base_url = if cluster.0 == "localhost" {
8+
let api_base_url = if cluster.0 == "localhost" {
99
format!("http://{}", cluster.0)
1010
} else {
1111
format!("https://{}", cluster.0)
1212
};
13-
println!("{}", base_url);
14-
let login_url = format!("{}/auth/login/ssh", base_url);
13+
let login_url = format!("{}/auth/login/ssh", api_base_url);
1514
let response = ApiClient::default()?
1615
.post(login_url)
1716
.bearer_auth(ApiClient::bearer_ssh_token(
@@ -22,14 +21,10 @@ pub fn command(name: Option<String>) -> anyhow::Result<()> {
2221
let status_code = response.status();
2322
if status_code.is_success() {
2423
let session = response.json::<SessionCredentials>()?;
25-
let base_dashboard_url = if cluster.0 == "localhost" {
26-
format!("http://{}:8844", cluster.0)
27-
} else {
28-
format!("https://{}", cluster.0.replace("api", "dashboard"))
29-
};
3024
let dashboard_login_url = format!(
31-
"{}/login?session={}",
32-
base_dashboard_url,
25+
"{}/login?api_base_url={}&session={}",
26+
"http://localhost:8844",
27+
api_base_url,
3328
session.to_base64()?
3429
);
3530
if webbrowser::open(&dashboard_login_url).is_ok() {

dashboard/.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
NEXT_PUBLIC_API_BASE_URL=http://127.0.0.1
21
NEXT_PUBLIC_DISCORD_INVITE=https://discord.gg/BP5aUkhcAh
32
NEXT_PUBLIC_DOCS_BASE_URL=https://dosei.io/docs

dashboard/src/app/ConsoleLayout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export default function ConsoleLayout({ children }) {
155155
<div className="flex items-center gap-5">
156156
<div className="hidden min-[416px]:contents">
157157
<div className="text-sm">
158-
Connected to: {infoQuery.data.name.replace("api.", "")}; Version: {infoQuery.data.version}
158+
Connected to: {infoQuery.data.name}; Version: {infoQuery.data.version}
159159
</div>
160160
<Link target="_blank" href={process.env.NEXT_PUBLIC_DISCORD_INVITE} className={clsx(
161161
"rounded-md px-2.5 py-1.5 text-sm",

dashboard/src/app/login/page.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,33 @@ import {HeroPattern} from "@/components/HeroPattern";
55
import {Footer} from "@/components/Footer";
66
import {useRouter, useSearchParams} from "next/navigation";
77
import {useEffect, Suspense} from "react";
8-
import {storeSessionFromString, getSession } from "@/utils/sessionUtils";
8+
import {storeSessionFromString, getSession, storeApiBaseURL} from "@/utils/sessionUtils";
99

1010
// Separate the component that uses useSearchParams
1111
function AuthHandler() {
1212
const router = useRouter();
1313
const searchParams = useSearchParams();
14+
const apiBaseUrl = searchParams.get('api_base_url');
1415
const session = searchParams.get('session');
1516

1617
useEffect(() => {
18+
if (apiBaseUrl) {
19+
storeApiBaseURL(apiBaseUrl);
20+
}
1721
const handleAsync = async () => {
1822
if (session) {
1923
try {
20-
// Decode base64 session parameter
2124
const decodedSession = atob(session);
2225
storeSessionFromString(decodedSession);
2326
await router.push("/");
2427
} catch (error) {
2528
console.error('Failed to decode session:', error);
2629
}
2730
} else {
28-
// Check if user is already authenticated via getSession
2931
try {
3032
const existingSession = getSession();
3133

3234
if (existingSession) {
33-
// User is already authenticated, redirect to /
3435
await router.push("/");
3536
}
3637
} catch (error) {
@@ -40,7 +41,7 @@ function AuthHandler() {
4041
}
4142
};
4243
handleAsync();
43-
}, [session, router]);
44+
}, [apiBaseUrl, session, router]);
4445

4546
return null; // This component only handles the auth logic
4647
}

dashboard/src/utils/sessionUtils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ export const getContinuePathAndDelete = () => {
2929
return continueURI;
3030
};
3131

32+
export const storeApiBaseURL = (apiBaseURL) => {
33+
localStorage.setItem("api_base_url", apiBaseURL);
34+
};
35+
3236
export const getApiBaseURL = () => {
3337
if (typeof window !== 'undefined') {
3438
const apiBaseURL = localStorage.getItem("api_base_url");
3539
if (apiBaseURL) {
3640
return apiBaseURL;
3741
}
3842
}
39-
return process.env.NEXT_PUBLIC_API_BASE_URL;
43+
return "http://127.0.0.1";
4044
};

0 commit comments

Comments
 (0)