diff --git a/apps/sim/lib/auth/connectors/providers.ts b/apps/sim/lib/auth/connectors/providers.ts index 314e71b0631..7167aa890f8 100644 --- a/apps/sim/lib/auth/connectors/providers.ts +++ b/apps/sim/lib/auth/connectors/providers.ts @@ -1754,6 +1754,7 @@ export function buildConnectorProviders(): GenericOAuthConfig[] { pkce: true, authentication: 'post', redirectURI: `${getBaseUrl()}/api/auth/oauth2/callback/monday`, + authorizationUrlParams: { force_install_if_needed: 'true' }, getToken: async ({ code, codeVerifier, redirectURI }) => { if (!codeVerifier) { throw new Error('Monday OAuth token exchange requires a PKCE verifier') diff --git a/apps/sim/lib/oauth/oauth.test.ts b/apps/sim/lib/oauth/oauth.test.ts index d5411450953..d829a91b48b 100644 --- a/apps/sim/lib/oauth/oauth.test.ts +++ b/apps/sim/lib/oauth/oauth.test.ts @@ -1,5 +1,7 @@ import { createMockFetch, resetEnvMock, setEnv } from '@sim/testing' -import { createAuthorizationURL, getOAuth2Tokens } from 'better-auth/oauth2' +import { getOAuth2Tokens } from 'better-auth/oauth2' +import { genericOAuth } from 'better-auth/plugins' +import { getTestInstance } from 'better-auth/test' import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' beforeAll(() => { @@ -178,26 +180,27 @@ describe('Monday OAuth connector', () => { pkce: true, authentication: 'post', redirectURI: 'http://localhost:3000/api/auth/oauth2/callback/monday', + authorizationUrlParams: { force_install_if_needed: 'true' }, }) - const authorizationUrl = await createAuthorizationURL({ - id: connector.providerId, - options: { - clientId: connector.clientId, - clientSecret: connector.clientSecret, - redirectURI: connector.redirectURI, + const { auth, signInWithTestUser } = await getTestInstance({ + baseURL: 'http://localhost:3000', + plugins: [genericOAuth({ config: [connector] })], + }) + const { headers } = await signInWithTestUser() + const { url } = await auth.api.oAuth2LinkAccount({ + body: { + providerId: connector.providerId, + callbackURL: 'http://localhost:3000/workspace', }, - authorizationEndpoint: connector.authorizationUrl!, - state: 'state-1', - codeVerifier: 'a'.repeat(128), - scopes: connector.scopes, - redirectURI: connector.redirectURI!, - responseType: connector.responseType, + headers, }) + const authorizationUrl = new URL(url) expect(authorizationUrl.searchParams.get('redirect_uri')).toBe( 'http://localhost:3000/api/auth/oauth2/callback/monday' ) expect(authorizationUrl.searchParams.get('scope')).toBe(connector.scopes?.join(' ')) + expect(authorizationUrl.searchParams.get('force_install_if_needed')).toBe('true') expect(authorizationUrl.searchParams.get('code_challenge_method')).toBe('S256') expect(authorizationUrl.searchParams.get('code_challenge')).toBeTruthy() })