diff --git a/src/browser/api.ts b/src/browser/api.ts index 390dd7449..77e76ca1b 100644 --- a/src/browser/api.ts +++ b/src/browser/api.ts @@ -1,3 +1,4 @@ +import { getBasepath } from "hookrouter" import { Application, ApplicationsResponse, CreateSessionResponse, FilesResponse, RecentResponse } from "../common/api" import { ApiEndpoint, HttpCode, HttpError } from "../common/http" @@ -18,7 +19,7 @@ export function setAuthed(authed: boolean): void { * Also set authed to false if the request returns unauthorized. */ const tryRequest = async (endpoint: string, options?: RequestInit): Promise => { - const response = await fetch("/api" + endpoint + "/", options) + const response = await fetch(getBasepath() + "/api" + endpoint + "/", options) if (response.status === HttpCode.Unauthorized) { setAuthed(false) } @@ -33,14 +34,9 @@ const tryRequest = async (endpoint: string, options?: RequestInit): Promise => { - let formBody: URLSearchParams | undefined - if (body) { - formBody = new URLSearchParams() - formBody.append("password", body.password) - } const response = await tryRequest(ApiEndpoint.login, { method: "POST", - body: formBody, + body: JSON.stringify({ ...body, basePath: getBasepath() }), headers: { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", }, diff --git a/src/browser/app.tsx b/src/browser/app.tsx index 2f12489f1..b3f1271f8 100644 --- a/src/browser/app.tsx +++ b/src/browser/app.tsx @@ -1,4 +1,4 @@ -import { getBasepath, navigate } from "hookrouter" +import { getBasepath, navigate, setBasepath } from "hookrouter" import * as React from "react" import { Application, isExecutableApplication } from "../common/api" import { HttpError } from "../common/http" @@ -11,25 +11,36 @@ export interface AppProps { } const App: React.FunctionComponent = (props) => { - const [authed, setAuthed] = React.useState(!!props.options.authed) + const [authed, setAuthed] = React.useState(props.options.authed) const [app, setApp] = React.useState(props.options.app) const [error, setError] = React.useState() + if (typeof window !== "undefined") { + const url = new URL(window.location.origin + window.location.pathname + props.options.basePath) + setBasepath(normalize(url.pathname)) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(window as any).setAuthed = (a: boolean): void => { + if (authed !== a) { + setAuthed(a) + // TEMP: Remove when no longer auto-loading VS Code. + if (a && !app) { + setApp({ + name: "VS Code", + path: "/", + embedPath: "/vscode-embed", + }) + } + } + } + } + React.useEffect(() => { if (app && !isExecutableApplication(app)) { navigate(normalize(`${getBasepath()}/${app.path}/`, true)) } }, [app]) - if (typeof window !== "undefined") { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ;(window as any).setAuthed = (a: boolean): void => { - if (authed !== a) { - setAuthed(a) - } - } - } - return ( <> {!app || !app.loaded ? ( @@ -41,7 +52,7 @@ const App: React.FunctionComponent = (props) => { )} {authed && app && app.embedPath ? ( - + ) : ( undefined )} diff --git a/src/browser/components/modal.tsx b/src/browser/components/modal.tsx index d57afc182..df8c1881f 100644 --- a/src/browser/components/modal.tsx +++ b/src/browser/components/modal.tsx @@ -128,7 +128,6 @@ export const Modal: React.FunctionComponent = (props) => {