diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f4213de9f..f7581221e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -33,11 +33,10 @@ fn read_txt_file(path: &str) -> Result { /// This command instantiates a new window with auth. /// The string returned from this method is the access token. #[tauri::command] -async fn login(app: tauri::AppHandle) -> Result { +async fn login(app: tauri::AppHandle, host: &str) -> Result { println!("Logging in..."); // Do an OAuth 2.0 Device Authorization Grant dance to get a token. - let host = "https://api.dev.kittycad.io/"; - let device_auth_url = oauth2::DeviceAuthorizationUrl::new(format!("{host}oauth2/device/auth")) + let device_auth_url = oauth2::DeviceAuthorizationUrl::new(format!("{host}/oauth2/device/auth")) .map_err(|e| InvokeError::from_anyhow(e.into()))?; // We can hardcode the client ID. // This value is safe to be embedded in version control. @@ -46,10 +45,10 @@ async fn login(app: tauri::AppHandle) -> Result { let auth_client = oauth2::basic::BasicClient::new( oauth2::ClientId::new(client_id), None, - oauth2::AuthUrl::new(format!("{host}authorize")) + oauth2::AuthUrl::new(format!("{host}/authorize")) .map_err(|e| InvokeError::from_anyhow(e.into()))?, Some( - oauth2::TokenUrl::new(format!("{host}oauth2/device/token")) + oauth2::TokenUrl::new(format!("{host}/oauth2/device/token")) .map_err(|e| InvokeError::from_anyhow(e.into()))?, ), ) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index c9973525b..61a3ca27a 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -48,7 +48,7 @@ "icons/icon.icns", "icons/icon.ico" ], - "identifier": "untitled-app", + "identifier": "KittyCAD-modeling-app", "longDescription": "", "macOS": { "entitlements": null, diff --git a/src/Auth.tsx b/src/Auth.tsx index 846f05abf..dc1d6ef5e 100644 --- a/src/Auth.tsx +++ b/src/Auth.tsx @@ -21,12 +21,14 @@ export const Auth = ({ children }: React.PropsWithChildren) => { if (user && 'id' in user) setUser(user) }, [user, setUser]) - if ( - (isTauri() && !token) || - (!isTauri() && !isLoading && !(user && 'id' in user)) - ) { - navigate('/signin') - } + useEffect(() => { + if ( + (isTauri() && !token) || + (!isTauri() && !isLoading && !(user && 'id' in user)) + ) { + navigate('/signin') + } + }, [user, token, navigate, isLoading]) return isLoading ? <>Loading... : <>{children} } diff --git a/src/routes/SignIn.tsx b/src/routes/SignIn.tsx index 26a94099c..514ca4230 100644 --- a/src/routes/SignIn.tsx +++ b/src/routes/SignIn.tsx @@ -4,7 +4,7 @@ import { isTauri } from '../lib/isTauri' import { useStore } from '../useStore' import { invoke } from '@tauri-apps/api/tauri' import { useNavigate } from 'react-router-dom' -import { VITE_KC_SITE_BASE_URL } from '../env' +import { VITE_KC_SITE_BASE_URL, VITE_KC_API_BASE_URL } from '../env' const SignIn = () => { const navigate = useNavigate() @@ -14,7 +14,9 @@ const SignIn = () => { const signInTauri = async () => { // We want to invoke our command to login via device auth. try { - const token: string = await invoke('login') + const token: string = await invoke('login', { + host: VITE_KC_API_BASE_URL, + }) setToken(token) navigate('/') } catch (error) {