fix build login (#237)

* fix build login

* fmt
This commit is contained in:
Kurt Hutten
2023-08-08 09:06:14 +10:00
committed by GitHub
parent 1cba48f513
commit ca985dd1a8
4 changed files with 17 additions and 14 deletions

View File

@ -33,11 +33,10 @@ fn read_txt_file(path: &str) -> Result<String, InvokeError> {
/// 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<String, InvokeError> {
async fn login(app: tauri::AppHandle, host: &str) -> Result<String, InvokeError> {
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<String, InvokeError> {
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()))?,
),
)

View File

@ -48,7 +48,7 @@
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "untitled-app",
"identifier": "KittyCAD-modeling-app",
"longDescription": "",
"macOS": {
"entitlements": null,

View File

@ -21,12 +21,14 @@ export const Auth = ({ children }: React.PropsWithChildren) => {
if (user && 'id' in user) setUser(user)
}, [user, setUser])
useEffect(() => {
if (
(isTauri() && !token) ||
(!isTauri() && !isLoading && !(user && 'id' in user))
) {
navigate('/signin')
}
}, [user, token, navigate, isLoading])
return isLoading ? <>Loading...</> : <>{children}</>
}

View File

@ -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) {