Skip to content

Commit

Permalink
feature: extenstion checks token-secret and database-id in indexDB be…
Browse files Browse the repository at this point in the history
…fore switch popup pages
  • Loading branch information
HenryC-3 committed Mar 8, 2023
1 parent 0002e03 commit e850aa5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/background/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ import { ActionForOneTimeMessages } from "../types";
import { createStore } from "../store";
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
import { addBook, getAuthInfo, saveAuthInfo } from "./utils";
import { ExtensionError } from "../../types";

const store = createStore();

export const actions: ActionForOneTimeMessages = {
checkAuthInfoExist: async function handleCheck(
messageValue,
backgroundResponse
) {
try {
const response = await getAuthInfo();
if (response) {
// TODO
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
backgroundResponse({ exist: true });
}
} catch (error) {
backgroundResponse({ error: error as ExtensionError });
}
},
checkAndSaveAuth: async function handleAuth(
messageValue,
backgroundResponse
Expand Down
2 changes: 1 addition & 1 deletion src/popup/components/AuthPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const handleClick = () => {
{
successAction: () => {
loadingStatus.value = false;
router.push("/save");
router.push("/");
},
failedAction: () => {
loadingStatus.value = false;
Expand Down
19 changes: 17 additions & 2 deletions src/popup/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@ import AddBookPage from "./components/AddBookPage.vue";
import OpenBookPage from "./components/OpenBookPage.vue";
import ErrorPage from "./components/ErrorPage.vue";
import AuthPage from "./components/AuthPage.vue";
import { sendToBackground } from "./messages";

const routes = [
{ path: "/save", component: AddBookPage },
{ path: "/", component: AddBookPage },
{ path: "/open", component: OpenBookPage },
{ path: "/error", component: ErrorPage },
{ path: "/", component: AuthPage },
{ path: "/auth", component: AuthPage },
];

export const router = createRouter({
history: createWebHashHistory(),
routes,
});

router.beforeEach(() => {
sendToBackground(
{ checkAuthInfoExist: true },
{
successAction: () => {
return true;
},
failedAction: () => {
router.push("/auth");
},
}
);
});
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface OneTimeMessage {
* receiver: background script
* notify background script check the token secret and database id provided by user */
checkAndSaveAuth?: { tokenSecret: string; databaseID: string };
/* sender: popup script
* receiver: background script
* notify background script check the token secret and database id is available in indexDB*/
checkAuthInfoExist?: boolean;
}

export type ExtensionError =
Expand Down

0 comments on commit e850aa5

Please sign in to comment.