diff --git a/packages/backend/src/graphql/mutation-resolvers.js b/packages/backend/src/graphql/mutation-resolvers.js index 2e9a697f..2c2379ec 100644 --- a/packages/backend/src/graphql/mutation-resolvers.js +++ b/packages/backend/src/graphql/mutation-resolvers.js @@ -2,12 +2,10 @@ import verifyConnection from './mutations/verify-connection.js'; import updateCurrentUser from './mutations/update-current-user.js'; import generateAuthUrl from './mutations/generate-auth-url.js'; -import createConnection from './mutations/create-connection.js'; import resetConnection from './mutations/reset-connection.js'; import updateConnection from './mutations/update-connection.js'; const mutationResolvers = { - createConnection, generateAuthUrl, resetConnection, updateConnection, diff --git a/packages/backend/src/graphql/mutations/create-connection.js b/packages/backend/src/graphql/mutations/create-connection.js deleted file mode 100644 index 2378a0eb..00000000 --- a/packages/backend/src/graphql/mutations/create-connection.js +++ /dev/null @@ -1,48 +0,0 @@ -import App from '../../models/app.js'; -import AppConfig from '../../models/app-config.js'; - -const createConnection = async (_parent, params, context) => { - context.currentUser.can('create', 'Connection'); - - const { key, appAuthClientId } = params.input; - - const app = await App.findOneByKey(key); - - const appConfig = await AppConfig.query().findOne({ key }); - - let formattedData = params.input.formattedData; - if (appConfig) { - if (appConfig.disabled) - throw new Error( - 'This application has been disabled for new connections!' - ); - - if (!appConfig.allowCustomConnection && formattedData) - throw new Error(`Custom connections cannot be created for ${app.name}!`); - - if (appConfig.shared && !formattedData) { - const authClient = await appConfig - .$relatedQuery('appAuthClients') - .findById(appAuthClientId) - .where({ - active: true, - }) - .throwIfNotFound(); - - formattedData = authClient.formattedAuthDefaults; - } - } - - const createdConnection = await context.currentUser - .$relatedQuery('connections') - .insert({ - key, - appAuthClientId, - formattedData, - verified: false, - }); - - return createdConnection; -}; - -export default createConnection; diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index b727fe7f..27c5d980 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -2,7 +2,6 @@ type Query { placeholderQuery(name: String): Boolean } type Mutation { - createConnection(input: CreateConnectionInput): Connection generateAuthUrl(input: GenerateAuthUrlInput): AuthLink resetConnection(input: ResetConnectionInput): Connection updateConnection(input: UpdateConnectionInput): Connection @@ -202,12 +201,6 @@ type SamlAuthProvidersRoleMapping { remoteRoleName: String } -input CreateConnectionInput { - key: String! - appAuthClientId: String - formattedData: JSONObject -} - input GenerateAuthUrlInput { id: String! } diff --git a/packages/web/src/graphql/mutations/create-connection.js b/packages/web/src/graphql/mutations/create-connection.js deleted file mode 100644 index e3fb37c3..00000000 --- a/packages/web/src/graphql/mutations/create-connection.js +++ /dev/null @@ -1,13 +0,0 @@ -import { gql } from '@apollo/client'; -export const CREATE_CONNECTION = gql` - mutation CreateConnection($input: CreateConnectionInput) { - createConnection(input: $input) { - id - key - verified - formattedData { - screenName - } - } - } -`; diff --git a/packages/web/src/graphql/mutations/index.js b/packages/web/src/graphql/mutations/index.js index fe596843..07e46710 100644 --- a/packages/web/src/graphql/mutations/index.js +++ b/packages/web/src/graphql/mutations/index.js @@ -1,10 +1,9 @@ -import { CREATE_CONNECTION } from './create-connection'; import { UPDATE_CONNECTION } from './update-connection'; import { VERIFY_CONNECTION } from './verify-connection'; import { RESET_CONNECTION } from './reset-connection'; import { GENERATE_AUTH_URL } from './generate-auth-url'; + const mutations = { - createConnection: CREATE_CONNECTION, updateConnection: UPDATE_CONNECTION, verifyConnection: VERIFY_CONNECTION, resetConnection: RESET_CONNECTION, diff --git a/packages/web/src/hooks/useAuthenticateApp.ee.js b/packages/web/src/hooks/useAuthenticateApp.ee.js index a4cca1cd..c140c3e7 100644 --- a/packages/web/src/hooks/useAuthenticateApp.ee.js +++ b/packages/web/src/hooks/useAuthenticateApp.ee.js @@ -7,6 +7,7 @@ import { } from 'helpers/authenticationSteps'; import computeAuthStepVariables from 'helpers/computeAuthStepVariables'; import useAppAuth from './useAppAuth'; +import useCreateConnection from './useCreateConnection'; import useFormatMessage from './useFormatMessage'; function getSteps(auth, hasConnection, useShared) { @@ -27,6 +28,7 @@ function getSteps(auth, hasConnection, useShared) { export default function useAuthenticateApp(payload) { const { appKey, appAuthClientId, connectionId, useShared = false } = payload; const { data: auth } = useAppAuth(appKey); + const { mutateAsync: createConnection } = useCreateConnection(appKey); const [authenticationInProgress, setAuthenticationInProgress] = React.useState(false); const formatMessage = useFormatMessage(); @@ -65,8 +67,13 @@ export default function useAuthenticateApp(payload) { } if (step.type === 'mutation') { - const stepResponse = await processMutation(step.name, variables); - response[step.name] = stepResponse; + if (step.name === 'createConnection') { + const stepResponse = await createConnection(variables); + response[step.name] = stepResponse?.data; + } else { + const stepResponse = await processMutation(step.name, variables); + response[step.name] = stepResponse; + } } else if (step.type === 'openWithPopup') { const stepResponse = await processPopupMessage(popup); response[step.name] = stepResponse; diff --git a/packages/web/src/hooks/useCreateConnection.js b/packages/web/src/hooks/useCreateConnection.js new file mode 100644 index 00000000..afcbe458 --- /dev/null +++ b/packages/web/src/hooks/useCreateConnection.js @@ -0,0 +1,26 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query'; + +import api from 'helpers/api'; + +export default function useCreateConnection(appKey) { + const queryClient = useQueryClient(); + + const query = useMutation({ + mutationFn: async ({ appAuthClientId, formattedData }) => { + const { data } = await api.post(`/v1/apps/${appKey}/connections`, { + appAuthClientId, + formattedData, + }); + + return data; + }, + + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ['apps', appKey, 'connections'], + }); + }, + }); + + return query; +}