oauth redirect

This commit is contained in:
hmacr committed 2025-03-07 00:01:12 +05:30
1 parent 8da6b3fc8b
commit a9612f1e61
4 files changed
+66 -14

No files matched your search

+4
View File
@@ -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
+8 -14
View File
@@ -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 (
<Button
type="submit"
onClick={() => {
dialogs.openModal({
title: t("Import data"),
content: <ImportNotionDialog />,
});
}}
neutral
>
{t("Import")}…
<Button type="submit" onClick={() => redirectTo(authUrl)} neutral>
{t("Import")}
</Button>
);
});
+14
View File
@@ -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();
+40
View File
@@ -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)}`;
}
}