From 176d2d639413cfb88a8d9f1efcc72ef70208518b Mon Sep 17 00:00:00 2001 From: Kurt Hutten Date: Mon, 6 Mar 2023 18:18:01 +1100 Subject: [PATCH] initial Auth implementation (#44) * initial implementation * update api url --- package.json | 1 + src/Auth.tsx | 31 +++++++++++++++++++++++++++++++ src/index.tsx | 4 ++-- src/lib/fetcher.ts | 8 ++++++++ src/lib/withBaseURL.ts | 4 ++++ yarn.lock | 9 ++++++++- 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/Auth.tsx create mode 100644 src/lib/fetcher.ts create mode 100644 src/lib/withBaseURL.ts diff --git a/package.json b/package.json index 97a82cde5..f32c308ca 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "react-dom": "^18.2.0", "react-json-view": "^1.21.3", "react-scripts": "5.0.1", + "swr": "^2.0.4", "three": "^0.146.0", "typescript": "^4.4.2", "util": "^0.12.5", diff --git a/src/Auth.tsx b/src/Auth.tsx new file mode 100644 index 000000000..87c97af45 --- /dev/null +++ b/src/Auth.tsx @@ -0,0 +1,31 @@ +import useSWR from 'swr' +import fetcher from './lib/fetcher' +import withBaseUrl from './lib/withBaseURL' +import App from './App' + +export const Auth = () => { + const { data: user, error } = useSWR(withBaseUrl('/user'), fetcher) as any + const isLocalHost = + typeof window !== 'undefined' && window.location.hostname === 'localhost' + + if (!user && !isLocalHost) { + return ( + <> +
+ + Sign in + +
+ + ) + } + + return +} diff --git a/src/index.tsx b/src/index.tsx index 1cc146d1e..92e0349f7 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,13 +1,13 @@ import React from 'react' import ReactDOM from 'react-dom/client' import './index.css' -import App from './App' +import { Auth } from './Auth' import reportWebVitals from './reportWebVitals' const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement) root.render( - + ) diff --git a/src/lib/fetcher.ts b/src/lib/fetcher.ts new file mode 100644 index 000000000..55cf3add4 --- /dev/null +++ b/src/lib/fetcher.ts @@ -0,0 +1,8 @@ +export default async function fetcher( + input: RequestInfo, + init: RequestInit = {} +): Promise { + const credentials = 'include' + const res = await fetch(input, { ...init, credentials }) + return res.json() +} diff --git a/src/lib/withBaseURL.ts b/src/lib/withBaseURL.ts new file mode 100644 index 000000000..cd0774181 --- /dev/null +++ b/src/lib/withBaseURL.ts @@ -0,0 +1,4 @@ +export default function withBaseUrl(path: string): string { + const baseUrl = 'https://dev.api.kittycad.io' + return baseUrl + path +} diff --git a/yarn.lock b/yarn.lock index 1b5be7f79..2f21d5c1c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9746,6 +9746,13 @@ svgo@^2.7.0: picocolors "^1.0.0" stable "^0.1.8" +swr@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/swr/-/swr-2.0.4.tgz#e68a5cc2e87b2a7f5f7ebf8a472cea24fc1c5fce" + integrity sha512-4GUiTjknRUVuw4MWUHR7mzJ9G/DWL+yZz/TgWDfiA0OZ9tL6qyrTkN2wPeboBpL3OJTkej3pexh3mWCnv8cFkQ== + dependencies: + use-sync-external-store "^1.2.0" + symbol-tree@^3.2.4: version "3.2.4" resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2" @@ -10250,7 +10257,7 @@ use-resize-observer@^9.0.0: dependencies: "@juggle/resize-observer" "^3.3.1" -use-sync-external-store@1.2.0: +use-sync-external-store@1.2.0, use-sync-external-store@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a" integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==