From ba0ab63c6f4a698113d04568bf5762b969852585 Mon Sep 17 00:00:00 2001 From: fengkx Date: Wed, 25 Aug 2021 23:39:06 +0800 Subject: [PATCH] fix: login state --- components/Editor/adapters/AdapterContext.tsx | 21 ++++++++++++++++--- components/Editor/adapters/memory.ts | 1 + components/Editor/adapters/supabase.ts | 1 + components/LeftAside/index.tsx | 5 +++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/components/Editor/adapters/AdapterContext.tsx b/components/Editor/adapters/AdapterContext.tsx index 8ecf887..5247d69 100644 --- a/components/Editor/adapters/AdapterContext.tsx +++ b/components/Editor/adapters/AdapterContext.tsx @@ -9,9 +9,24 @@ const AdapterContext = createContext(memoryAdapter); export type PropsType = { adapter?: IAdapter; }; +export function getParameterByName(name: string, url?: string): string | null { + if (!url) url = window.location.href; + name = name.replace(/[\[\]]/g, "\\$&"); + var regex = new RegExp("[?&#]" + name + "(=([^&#]*)|&|#|$)"), + results = regex.exec(url); + if (!results) return null; + if (!results[2]) return ""; + return decodeURIComponent(results[2].replace(/\+/g, " ")); +} + export const AdapterProvider: React.FC = ({ children, adapter }) => { if (!adapter) { if (hasSupabase) { + const access_token = + typeof window !== "undefined" && getParameterByName("access_token"); + if (access_token) { + supabase.auth.setAuth(access_token); + } const session = supabase.auth.session(); console.log(session, Boolean(session)); adapter = Boolean(session) ? supbaseAdapter : memoryAdapter; @@ -24,10 +39,10 @@ export const AdapterProvider: React.FC = ({ children, adapter }) => { useEffect(() => { if (hasSupabase) { const session = supabase.auth.session(); - - setAdapterState(Boolean(session) ? supbaseAdapter : memoryAdapter); + const adapter = Boolean(session) ? supbaseAdapter : memoryAdapter; + setAdapterState(adapter); } - }, []); + }); return ( {children} diff --git a/components/Editor/adapters/memory.ts b/components/Editor/adapters/memory.ts index bd06b56..e70181a 100644 --- a/components/Editor/adapters/memory.ts +++ b/components/Editor/adapters/memory.ts @@ -314,6 +314,7 @@ const gotoPageAtom = atom(null, (get, set, update) => { }); export const memoryAdapter = { + $$type: "memory" as "memory" | "supabase", pageFamily, pageIdAtom, pagesAtom, diff --git a/components/Editor/adapters/supabase.ts b/components/Editor/adapters/supabase.ts index a1db5cd..0920604 100644 --- a/components/Editor/adapters/supabase.ts +++ b/components/Editor/adapters/supabase.ts @@ -404,6 +404,7 @@ const gotoPageAtom = atom( ); export const supbaseAdapter: TSupabaseAdapter = { + $$type: "supabase", isStaleAtom, newPageAtom, newBlockAtom, diff --git a/components/LeftAside/index.tsx b/components/LeftAside/index.tsx index 572826f..5052cf0 100644 --- a/components/LeftAside/index.tsx +++ b/components/LeftAside/index.tsx @@ -9,7 +9,8 @@ import { ToolButton } from "./ToolButton"; import { DotFlashing } from "../Loading"; import { hasSupabase, supabase } from "../../db"; import clsx from "clsx"; -import { useMountEffect, useToggle } from "@react-hookz/web"; +import { useToggle } from "@react-hookz/web"; + export const LeftAside: React.FC = () => { const router = useRouter(); const { gotoPageAtom, loadNotesAtom, saveNotesAtom } = useAdapter(); @@ -92,7 +93,7 @@ export const LeftAside: React.FC = () => { imgHeight={24} onClick={async () => { await supabase.auth.signOut(); - router.reload(); + setTimeout(router.reload, 0); }} /> ) : (