feat(notion): add auth and new DB items trigger

This commit is contained in:
Ali BARIN
2023-06-12 22:45:37 +00:00
parent f2dc2f5530
commit 6be8b55daa
15 changed files with 351 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import { URL, URLSearchParams } from 'url';
export default async function generateAuthUrl($: IGlobalVariable) {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value as string;
const searchParams = new URLSearchParams({
client_id: $.auth.data.clientId as string,
redirect_uri: redirectUri,
response_type: 'code',
owner: 'user',
});
const url = new URL(`/v1/oauth/authorize?${searchParams}`, $.app.apiBaseUrl).toString();
await $.auth.set({
url,
});
}

View File

@@ -0,0 +1,49 @@
import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
export default {
fields: [
{
key: 'oAuthRedirectUrl',
label: 'OAuth Redirect URL',
type: 'string' as const,
required: true,
readOnly: true,
value: '{WEB_APP_URL}/app/notion/connections/add',
placeholder: null,
description:
'When asked to input an OAuth callback or redirect URL in Notion OAuth, enter the URL above.',
docUrl: 'https://automatisch.io/docs/notion#oauth-redirect-url',
clickToCopy: true,
},
{
key: 'clientId',
label: 'Client ID',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
docUrl: 'https://automatisch.io/docs/notion#client-id',
clickToCopy: false,
},
{
key: 'clientSecret',
label: 'Client Secret',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
docUrl: 'https://automatisch.io/docs/notion#client-secret',
clickToCopy: false,
},
],
generateAuthUrl,
verifyCredentials,
isStillVerified,
};

View File

@@ -0,0 +1,9 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
const isStillVerified = async ($: IGlobalVariable) => {
const user = await getCurrentUser($);
return !!user.id;
};
export default isStillVerified;

View File

@@ -0,0 +1,53 @@
import { IGlobalVariable, IField } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
const verifyCredentials = async ($: IGlobalVariable) => {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value as string;
const response = await $.http.post(
`${$.app.apiBaseUrl}/v1/oauth/token`,
{
redirect_uri: redirectUri,
code: $.auth.data.code,
grant_type: 'authorization_code',
},
{
headers: {
Authorization: `Basic ${Buffer.from(
$.auth.data.clientId + ':' + $.auth.data.clientSecret
).toString('base64')}`,
},
additionalProperties: {
skipAddingAuthHeader: true
}
}
);
const data = response.data;
$.auth.data.accessToken = data.access_token;
await $.auth.set({
clientId: $.auth.data.clientId,
clientSecret: $.auth.data.clientSecret,
accessToken: data.access_token,
botId: data.bot_id,
duplicatedTemplateId: data.duplicated_template_id,
owner: data.owner,
tokenType: data.token_type,
workspaceIcon: data.workspace_icon,
workspaceId: data.workspace_id,
workspaceName: data.workspace_name,
screenName: data.workspace_name,
});
const currentUser = await getCurrentUser($);
await $.auth.set({
screenName: `${currentUser.name} @ ${data.workspace_name}`,
});
};
export default verifyCredentials;