2023-03-06 18:18:01 +11:00
|
|
|
import useSWR from 'swr'
|
|
|
|
import fetcher from './lib/fetcher'
|
|
|
|
import withBaseUrl from './lib/withBaseURL'
|
2023-06-22 16:43:33 +10:00
|
|
|
import { App } from './App'
|
2023-07-11 20:34:09 +10:00
|
|
|
import { SetToken } from './components/TokenInput'
|
|
|
|
import { useStore } from './useStore'
|
2023-07-13 07:22:08 -04:00
|
|
|
import {
|
|
|
|
createBrowserRouter,
|
|
|
|
RouterProvider,
|
|
|
|
} from "react-router-dom"
|
|
|
|
import { ErrorPage } from './components/ErrorPage'
|
|
|
|
import { Settings } from './routes/Settings'
|
|
|
|
|
|
|
|
const router = createBrowserRouter([
|
|
|
|
{
|
|
|
|
path: "/",
|
|
|
|
element: <App />,
|
|
|
|
errorElement: <ErrorPage />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/settings",
|
|
|
|
element: <Settings />,
|
|
|
|
}
|
|
|
|
])
|
2023-03-06 18:18:01 +11:00
|
|
|
|
|
|
|
export const Auth = () => {
|
2023-03-06 20:13:34 +11:00
|
|
|
const { data: user } = useSWR(withBaseUrl('/user'), fetcher) as any
|
2023-07-11 20:34:09 +10:00
|
|
|
const {token} = useStore((s) => ({
|
|
|
|
token: s.token
|
|
|
|
}))
|
|
|
|
|
2023-03-06 18:18:01 +11:00
|
|
|
const isLocalHost =
|
|
|
|
typeof window !== 'undefined' && window.location.hostname === 'localhost'
|
|
|
|
|
2023-07-11 20:34:09 +10:00
|
|
|
if ((window as any).__TAURI__ && !token) {
|
|
|
|
return <SetToken />
|
|
|
|
}
|
|
|
|
|
2023-03-06 18:18:01 +11:00
|
|
|
if (!user && !isLocalHost) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className=" bg-gray-800 p-1 px-4 rounded-r-lg pointer-events-auto flex items-center">
|
|
|
|
<a
|
|
|
|
className="font-bold mr-2 text-purple-400"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
target={'_self'}
|
|
|
|
href={`https://dev.kittycad.io/signin?callbackUrl=${encodeURIComponent(
|
|
|
|
typeof window !== 'undefined' && window.location.href
|
|
|
|
)}`}
|
|
|
|
>
|
|
|
|
Sign in
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-13 07:22:08 -04:00
|
|
|
return <RouterProvider router={router} />
|
2023-03-06 18:18:01 +11:00
|
|
|
}
|