diff --git a/.env.sample b/.env.sample index 51046501da..8f2e4910e8 100644 --- a/.env.sample +++ b/.env.sample @@ -147,6 +147,10 @@ DISCORD_SERVER_ID= # DISCORD_SERVER_ID and DISCORD_SERVER_ROLES must be set together. DISCORD_SERVER_ROLES= +# –––––––––––––– IMPORTS –––––––––––––– +NOTION_CLIENT_ID= +NOTION_CLIENT_SECRET= + # –––––––––––––––– OPTIONAL –––––––––––––––– # Base64 encoded private key and certificate for HTTPS termination. This is only diff --git a/plugins/notion/client/Imports.tsx b/plugins/notion/client/Imports.tsx index 5420640755..d4e495dd84 100644 --- a/plugins/notion/client/Imports.tsx +++ b/plugins/notion/client/Imports.tsx @@ -1,26 +1,20 @@ import { observer } from "mobx-react"; import React from "react"; import { useTranslation } from "react-i18next"; -import ImportNotionDialog from "~/scenes/Settings/components/ImportNotionDialog"; import Button from "~/components/Button"; -import useStores from "~/hooks/useStores"; +import useCurrentTeam from "~/hooks/useCurrentTeam"; +import { redirectTo } from "~/utils/urls"; +import { NotionUtils } from "../shared/NotionUtils"; export const Notion = observer(() => { const { t } = useTranslation(); - const { dialogs } = useStores(); + const team = useCurrentTeam(); + + const authUrl = NotionUtils.authUrl({ state: { teamId: team.id } }); return ( - ); }); diff --git a/plugins/notion/server/env.ts b/plugins/notion/server/env.ts new file mode 100644 index 0000000000..6a135ad285 --- /dev/null +++ b/plugins/notion/server/env.ts @@ -0,0 +1,14 @@ +import { Environment } from "@server/env"; +import { Public } from "@server/utils/decorators/Public"; +import environment from "@server/utils/environment"; +import { CannotUseWithout } from "@server/utils/validators"; + +class NotionPluginEnvironment extends Environment { + @Public + public NOTION_CLIENT_ID = environment.NOTION_CLIENT_ID; + + @CannotUseWithout("NOTION_CLIENT_ID") + public NOTION_CLIENT_SECRET = environment.NOTION_CLIENT_SECRET; +} + +export default new NotionPluginEnvironment(); diff --git a/plugins/notion/shared/NotionUtils.ts b/plugins/notion/shared/NotionUtils.ts new file mode 100644 index 0000000000..81755e2420 --- /dev/null +++ b/plugins/notion/shared/NotionUtils.ts @@ -0,0 +1,40 @@ +import queryString from "query-string"; +import env from "@shared/env"; + +export type OAuthState = { + teamId: string; +}; + +export class NotionUtils { + // private static clientId = "eda3ac5b-39fa-4d79-a99d-b70bc5ded123"; + private static clientId = env.NOTION_CLIENT_ID; + private static clientSecret = env.NOTION_CLIENT_SECRET; + private static authBaseUrl = "https://api.notion.com/v1/oauth/authorize"; + + static parseState(state: string): OAuthState { + return JSON.parse(state); + } + + static callbackUrl( + { baseUrl, params }: { baseUrl: string; params?: string } = { + // baseUrl: `${env.URL}`, + baseUrl: "https://redirect-cf-worker.hmacr.workers.dev", + params: undefined, + } + ) { + return params + ? `${baseUrl}/api/notion.callback?${params}` + : `${baseUrl}/api/notion.callback`; + } + + static authUrl({ state }: { state: OAuthState }) { + const params = { + client_id: this.clientId, + redirect_uri: this.callbackUrl(), + state: JSON.stringify(state), + response_type: "code", + owner: "user", + }; + return `${this.authBaseUrl}?${queryString.stringify(params)}`; + } +}