diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 236f10fd6..5ca0ed8c6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -6,6 +6,7 @@ use std::io::Read; use anyhow::Result; use oauth2::TokenResponse; use tauri::{InvokeError, Manager}; +const DEFAULT_HOST: &str = "https://api.kittycad.io"; /// This command returns the a json string parse from a toml file at the path. #[tauri::command] @@ -88,11 +89,34 @@ async fn login(app: tauri::AppHandle, host: &str) -> Result ///This command returns the KittyCAD user info given a token. /// The string returned from this method is the user info as a json string. #[tauri::command] -async fn get_user(token: Option) -> Result { +async fn get_user( + token: Option, + hostname: &str, +) -> Result { + // Use the host passed in if it's set. + // Otherwise, use the default host. + let host = if hostname.is_empty() { + DEFAULT_HOST.to_string() + } else { + hostname.to_string() + }; + + // Change the baseURL to the one we want. + let mut baseurl = host.to_string(); + if !host.starts_with("http://") && !host.starts_with("https://") { + baseurl = format!("https://{host}"); + if host.starts_with("localhost") { + baseurl = format!("http://{host}") + } + } println!("Getting user info..."); // use kittycad library to fetch the user info from /user/me - let client = kittycad::Client::new(token.unwrap()); + let mut client = kittycad::Client::new(token.unwrap()); + + if baseurl != DEFAULT_HOST { + client.set_base_url(&baseurl); + } let user_info: kittycad::types::User = client .users() diff --git a/src/Auth.tsx b/src/Auth.tsx index 35c5d01ce..a18eb52aa 100644 --- a/src/Auth.tsx +++ b/src/Auth.tsx @@ -6,9 +6,9 @@ export const Auth = ({ children }: React.PropsWithChildren) => { const { auth: { state }, } = useGlobalStateContext() - const isLoggedIn = state.matches('checkIfLoggedIn') + const isLoggingIn = state.matches('checkIfLoggedIn') - return isLoggedIn ? ( + return isLoggingIn ? ( Loading KittyCAD Modeling App... ) : ( <>{children} diff --git a/src/machines/authMachine.ts b/src/machines/authMachine.ts index 3ce14b2e1..82289fcc1 100644 --- a/src/machines/authMachine.ts +++ b/src/machines/authMachine.ts @@ -4,6 +4,7 @@ import withBaseURL from '../lib/withBaseURL' import { CommandBarMeta } from '../lib/commands' import { isTauri } from 'lib/isTauri' import { invoke } from '@tauri-apps/api' +import { VITE_KC_API_BASE_URL } from 'env' const SKIP_AUTH = import.meta.env.VITE_KC_SKIP_AUTH === 'true' && import.meta.env.DEV @@ -132,6 +133,7 @@ async function getUser(context: UserContext) { .catch((err) => console.error('error from Browser getUser', err)) : invoke>('get_user', { token: context.token, + hostname: VITE_KC_API_BASE_URL, }).catch((err) => console.error('error from Tauri getUser', err)) const user = await userPromise