Skip to content

Commit

Permalink
fix: login state
Browse files Browse the repository at this point in the history
  • Loading branch information
fengkx committed Aug 25, 2021
1 parent e1434e2 commit ba0ab63
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
21 changes: 18 additions & 3 deletions components/Editor/adapters/AdapterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@ const AdapterContext = createContext<IAdapter>(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<PropsType> = ({ 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;
Expand All @@ -24,10 +39,10 @@ export const AdapterProvider: React.FC<PropsType> = ({ children, adapter }) => {
useEffect(() => {
if (hasSupabase) {
const session = supabase.auth.session();

setAdapterState(Boolean(session) ? supbaseAdapter : memoryAdapter);
const adapter = Boolean(session) ? supbaseAdapter : memoryAdapter;
setAdapterState(adapter);
}
}, []);
});
return (
<AdapterContext.Provider value={adapterState}>
{children}
Expand Down
1 change: 1 addition & 0 deletions components/Editor/adapters/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ const gotoPageAtom = atom<null, todayPageUpdate>(null, (get, set, update) => {
});

export const memoryAdapter = {
$$type: "memory" as "memory" | "supabase",
pageFamily,
pageIdAtom,
pagesAtom,
Expand Down
1 change: 1 addition & 0 deletions components/Editor/adapters/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ const gotoPageAtom = atom<null, todayPageUpdate>(
);

export const supbaseAdapter: TSupabaseAdapter = {
$$type: "supabase",
isStaleAtom,
newPageAtom,
newBlockAtom,
Expand Down
5 changes: 3 additions & 2 deletions components/LeftAside/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -92,7 +93,7 @@ export const LeftAside: React.FC = () => {
imgHeight={24}
onClick={async () => {
await supabase.auth.signOut();
router.reload();
setTimeout(router.reload, 0);
}}
/>
) : (
Expand Down

1 comment on commit ba0ab63

@vercel
Copy link

@vercel vercel bot commented on ba0ab63 Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.