@ -33,11 +33,10 @@ fn read_txt_file(path: &str) -> Result<String, InvokeError> {
|
|||||||
/// This command instantiates a new window with auth.
|
/// This command instantiates a new window with auth.
|
||||||
/// The string returned from this method is the access token.
|
/// The string returned from this method is the access token.
|
||||||
#[tauri::command]
|
#[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...");
|
println!("Logging in...");
|
||||||
// Do an OAuth 2.0 Device Authorization Grant dance to get a token.
|
// 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()))?;
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
||||||
// We can hardcode the client ID.
|
// We can hardcode the client ID.
|
||||||
// This value is safe to be embedded in version control.
|
// 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(
|
let auth_client = oauth2::basic::BasicClient::new(
|
||||||
oauth2::ClientId::new(client_id),
|
oauth2::ClientId::new(client_id),
|
||||||
None,
|
None,
|
||||||
oauth2::AuthUrl::new(format!("{host}authorize"))
|
oauth2::AuthUrl::new(format!("{host}/authorize"))
|
||||||
.map_err(|e| InvokeError::from_anyhow(e.into()))?,
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?,
|
||||||
Some(
|
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()))?,
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -48,7 +48,7 @@
|
|||||||
"icons/icon.icns",
|
"icons/icon.icns",
|
||||||
"icons/icon.ico"
|
"icons/icon.ico"
|
||||||
],
|
],
|
||||||
"identifier": "untitled-app",
|
"identifier": "KittyCAD-modeling-app",
|
||||||
"longDescription": "",
|
"longDescription": "",
|
||||||
"macOS": {
|
"macOS": {
|
||||||
"entitlements": null,
|
"entitlements": null,
|
||||||
|
@ -21,12 +21,14 @@ export const Auth = ({ children }: React.PropsWithChildren) => {
|
|||||||
if (user && 'id' in user) setUser(user)
|
if (user && 'id' in user) setUser(user)
|
||||||
}, [user, setUser])
|
}, [user, setUser])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
(isTauri() && !token) ||
|
(isTauri() && !token) ||
|
||||||
(!isTauri() && !isLoading && !(user && 'id' in user))
|
(!isTauri() && !isLoading && !(user && 'id' in user))
|
||||||
) {
|
) {
|
||||||
navigate('/signin')
|
navigate('/signin')
|
||||||
}
|
}
|
||||||
|
}, [user, token, navigate, isLoading])
|
||||||
|
|
||||||
return isLoading ? <>Loading...</> : <>{children}</>
|
return isLoading ? <>Loading...</> : <>{children}</>
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import { isTauri } from '../lib/isTauri'
|
|||||||
import { useStore } from '../useStore'
|
import { useStore } from '../useStore'
|
||||||
import { invoke } from '@tauri-apps/api/tauri'
|
import { invoke } from '@tauri-apps/api/tauri'
|
||||||
import { useNavigate } from 'react-router-dom'
|
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 SignIn = () => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
@ -14,7 +14,9 @@ const SignIn = () => {
|
|||||||
const signInTauri = async () => {
|
const signInTauri = async () => {
|
||||||
// We want to invoke our command to login via device auth.
|
// We want to invoke our command to login via device auth.
|
||||||
try {
|
try {
|
||||||
const token: string = await invoke('login')
|
const token: string = await invoke('login', {
|
||||||
|
host: VITE_KC_API_BASE_URL,
|
||||||
|
})
|
||||||
setToken(token)
|
setToken(token)
|
||||||
navigate('/')
|
navigate('/')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Reference in New Issue
Block a user