From 03dd382e69cde378be22727769e3e5b94f95ac8f Mon Sep 17 00:00:00 2001 From: softmarshmallow Date: Wed, 24 May 2023 19:40:49 +0900 Subject: [PATCH 1/7] add auth guard wrapper --- app/lib/main/index.tsx | 51 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/app/lib/main/index.tsx b/app/lib/main/index.tsx index 94fa35c4..466be028 100644 --- a/app/lib/main/index.tsx +++ b/app/lib/main/index.tsx @@ -26,7 +26,7 @@ import { ToolboxScreen } from "../pages/tool-box"; import { FontReplacerScreen } from "@toolbox/font-replacer"; import { CodeScreen } from "@app/design-to-code"; import { AboutScreen } from "../pages/about"; -import { SigninScreen } from "@app/auth"; +import { AuthGuard, SigninScreen } from "@app/auth"; import { UpgradePage } from "@assistant-fp/early-access"; import { ToolboxHome } from "@app/toolbox"; import { LiveSessionPage } from "@app/live"; @@ -310,28 +310,33 @@ export default function App(props: { platform: TargetPlatform }) { return ( - {/* @ts-ignore */} - - - {/* # region unique route section */} - {standalone_pages.map((p) => { - return ( - { - return ; - }} - /> - ); - })} - {/* # endregion unique route section */} - {/* dynamic route shall be placed at the last point, since it overwrites other routes */} - - - {/* 👆 this is for preventing blank page on book up. this will be fixed and removed.*/} - - + + {/* @ts-ignore */} + + + {/* # region unique route section */} + {standalone_pages.map((p) => { + return ( + { + return ; + }} + /> + ); + })} + {/* # endregion unique route section */} + {/* dynamic route shall be placed at the last point, since it overwrites other routes */} + + + {/* 👆 this is for preventing blank page on book up. this will be fixed and removed.*/} + + + ); From 290522ec7037bc67f1c4715032057db3c8f513ed Mon Sep 17 00:00:00 2001 From: softmarshmallow Date: Wed, 24 May 2023 19:41:02 +0900 Subject: [PATCH 2/7] split k --- packages/_firstparty-auth/index.ts | 3 +++ packages/_firstparty-auth/k.ts | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 packages/_firstparty-auth/k.ts diff --git a/packages/_firstparty-auth/index.ts b/packages/_firstparty-auth/index.ts index 1e3e2126..45e0515b 100644 --- a/packages/_firstparty-auth/index.ts +++ b/packages/_firstparty-auth/index.ts @@ -101,3 +101,6 @@ export async function getUserProfile() { throw error; } } + +export { AuthStorage } from "./storage"; +export * as k from "./k"; diff --git a/packages/_firstparty-auth/k.ts b/packages/_firstparty-auth/k.ts new file mode 100644 index 00000000..6015d846 --- /dev/null +++ b/packages/_firstparty-auth/k.ts @@ -0,0 +1,3 @@ +/** Preserve value: Do not change value */ +export const ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY = + "co.grida.assistant/user-auth"; From d80f777f993cd588cfcee97d4d4e48f7f90e99ad Mon Sep 17 00:00:00 2001 From: softmarshmallow Date: Wed, 24 May 2023 19:41:28 +0900 Subject: [PATCH 3/7] update auth storage to localstorage, without json format --- packages/_firstparty-auth/storage.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/_firstparty-auth/storage.ts b/packages/_firstparty-auth/storage.ts index 54628070..c8d6dbd5 100644 --- a/packages/_firstparty-auth/storage.ts +++ b/packages/_firstparty-auth/storage.ts @@ -1,20 +1,28 @@ -/// -/// -/// -import { PluginSdk } from "@plugin-sdk/app"; - -/** Preserve value: Do not change value */ -const _ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY = - "co.grida.assistant/user-auth"; +import * as k from "./k"; type Credential = string; function saveAuthCredential(credential: Credential) { - PluginSdk.setItem(_ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY, credential); + // usees localstorage + localStorage.setItem( + k.ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY, + credential + ); + + // propagate event (this is required since we're listening in same window) + // although, caution required since this will trigger multiple times if listening in other windows + // ref: https://stackoverflow.com/questions/35865481/storage-event-not-firing + const event = new StorageEvent("storage", { + key: k.ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY, + oldValue: null, + newValue: credential, + }); + window.dispatchEvent(event); } async function getAuthCredential(): Promise { - return PluginSdk.getItem(_ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY); + // usees localstorage + return localStorage.getItem(k.ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY); } function saveProfile(profile) { From 760bbf5f32618ff85623d55f00fe8eb1d2c522ea Mon Sep 17 00:00:00 2001 From: softmarshmallow Date: Wed, 24 May 2023 19:41:45 +0900 Subject: [PATCH 4/7] add auth state hook --- packages/app-auth/hooks/index.ts | 1 + .../hooks/use-assistant-auth-state.ts | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 packages/app-auth/hooks/index.ts create mode 100644 packages/app-auth/hooks/use-assistant-auth-state.ts diff --git a/packages/app-auth/hooks/index.ts b/packages/app-auth/hooks/index.ts new file mode 100644 index 00000000..e57f1e4c --- /dev/null +++ b/packages/app-auth/hooks/index.ts @@ -0,0 +1 @@ +export * from "./use-assistant-auth-state"; diff --git a/packages/app-auth/hooks/use-assistant-auth-state.ts b/packages/app-auth/hooks/use-assistant-auth-state.ts new file mode 100644 index 00000000..979734f6 --- /dev/null +++ b/packages/app-auth/hooks/use-assistant-auth-state.ts @@ -0,0 +1,36 @@ +import { useState, useEffect } from "react"; +import { AuthStorage, k } from "@assistant-fp/auth"; + +export function useAssistantAuthState() { + const [state, setState] = useState(null); + + const validate = (token: string) => { + // ping + // TODO: send auth ping + if (token) { + setState(true); + } else { + setState(false); + } + }; + + useEffect(() => { + // check initial state + AuthStorage.get().then(validate); + + const listener = (e: StorageEvent) => { + if (e.key === k.ASSISTANT_GRIDA_AUTHENTICATION_CREDENTIAL_KEY) { + validate(e.newValue); + } + }; + + // listen for storage events + window.addEventListener("storage", listener); + + return () => { + window.removeEventListener("storage", listener); + }; + }, []); + + return state; +} From a29f5433c5e0c8ca9a8a181347f71155a45a2382 Mon Sep 17 00:00:00 2001 From: softmarshmallow Date: Wed, 24 May 2023 19:42:03 +0900 Subject: [PATCH 5/7] optional back button on signin --- packages/app-auth/signin/signin-screen.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/app-auth/signin/signin-screen.tsx b/packages/app-auth/signin/signin-screen.tsx index 63897da7..922be0f2 100644 --- a/packages/app-auth/signin/signin-screen.tsx +++ b/packages/app-auth/signin/signin-screen.tsx @@ -100,13 +100,13 @@ export function SigninScreen({ const history = useHistory(); const close = () => { - onClose ? onClose?.() : history.goBack(); + onClose ? onClose?.() : history?.goBack?.(); }; return ( <> - + {onClose && } {!isAuthenticated ? ( !isLoading ? ( From e3457448b84f88faad13b3fc0b7fd7d01bf1b2f2 Mon Sep 17 00:00:00 2001 From: softmarshmallow Date: Wed, 24 May 2023 19:42:24 +0900 Subject: [PATCH 6/7] add auth guard wrapper component --- CHANGELOG.md | 7 ++++- packages/app-auth/guard/auth-guard.tsx | 41 ++++++++++++++++++++++++++ packages/app-auth/guard/index.ts | 1 + packages/app-auth/index.ts | 1 + 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 packages/app-auth/guard/auth-guard.tsx create mode 100644 packages/app-auth/guard/index.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b7140e7..73c8bcd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,12 @@ - [Flutter] Poligon Node support with XImage (svg) - [Lint] Primal naming & grouping linting for better code export quality. this is tracked sperately on [lint](https://github.com/bridgedxyz/lint) -## [2022.12.0.1] - 2022-12-6 (schduled) +## [2023.5.1] - 2023-05-24 + +- Signin is required to use Assistant + - Exsiting session might be expired + +## [2022.12.0.1] - 2022-12-6 - New Icons set added. - Unicon Icons diff --git a/packages/app-auth/guard/auth-guard.tsx b/packages/app-auth/guard/auth-guard.tsx new file mode 100644 index 00000000..e03bf2a6 --- /dev/null +++ b/packages/app-auth/guard/auth-guard.tsx @@ -0,0 +1,41 @@ +import React, { useState, useEffect } from "react"; +import { Dialog } from "@material-ui/core"; +import { SigninScreen } from "../signin/signin-screen"; +import { useAssistantAuthState } from "../hooks"; + +export function AuthGuard({ + children, + required, +}: React.PropsWithChildren<{ + required?: boolean; +}>) { + const authenticated = useAssistantAuthState(); + const [open, setOpen] = useState(false); + + useEffect(() => { + switch (authenticated) { + case null: + break; + case false: + setOpen(true); + break; + case true: + break; + } + }, [authenticated]); + + return ( + <> + {children} + {required && ( + + { + setOpen(false); + }} + /> + + )} + + ); +} diff --git a/packages/app-auth/guard/index.ts b/packages/app-auth/guard/index.ts new file mode 100644 index 00000000..d354c435 --- /dev/null +++ b/packages/app-auth/guard/index.ts @@ -0,0 +1 @@ +export * from "./auth-guard"; diff --git a/packages/app-auth/index.ts b/packages/app-auth/index.ts index 0a8ebd6e..357ebf0e 100644 --- a/packages/app-auth/index.ts +++ b/packages/app-auth/index.ts @@ -1 +1,2 @@ export { SigninScreen } from "./signin/signin-screen"; +export { AuthGuard } from "./guard"; From c5b04c3dffb696c67a335ad1bf66a1b4881b0ab7 Mon Sep 17 00:00:00 2001 From: softmarshmallow Date: Mon, 5 Jun 2023 09:48:29 +0900 Subject: [PATCH 7/7] bump base-sdk --- packages/base-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/base-sdk b/packages/base-sdk index 42e77b07..9b2bb811 160000 --- a/packages/base-sdk +++ b/packages/base-sdk @@ -1 +1 @@ -Subproject commit 42e77b0755053f0c0a98ce6137c706c16cbd11e9 +Subproject commit 9b2bb811b8ef1f8e02e5a50d46811df0dc2ca486