The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
feat(auth): add default workspace support for user handling (#9099)
Antoine Moreaux07bde4883e23
diff --git a/packages/twenty-front/src/generated/graphql.tsx b/packages/twenty-front/src/generated/graphql.tsxindex 73ff4e9954a..57a4082fb4d 100644--- a/packages/twenty-front/src/generated/graphql.tsx+++ b/packages/twenty-front/src/generated/graphql.tsx@@ -1333,6 +1333,7 @@ export type UserEdge = {export type UserExists = {__typename?: 'UserExists';availableWorkspaces: Array<AvailableWorkspaceOutput>;+ defaultWorkspaceId: Scalars['String'];exists: Scalars['Boolean'];};@@ -1927,7 +1928,7 @@ export type CheckUserExistsQueryVariables = Exact<{}>;-export type CheckUserExistsQuery = { __typename?: 'Query', checkUserExists: { __typename: 'UserExists', exists: boolean, availableWorkspaces: Array<{ __typename?: 'AvailableWorkspaceOutput', id: string, displayName?: string | null, subdomain: string, logo?: string | null, sso: Array<{ __typename?: 'SSOConnection', type: IdentityProviderType, id: string, issuer: string, name: string, status: SsoIdentityProviderStatus }> }> } | { __typename: 'UserNotExists', exists: boolean } };+export type CheckUserExistsQuery = { __typename?: 'Query', checkUserExists: { __typename: 'UserExists', exists: boolean, defaultWorkspaceId: string, availableWorkspaces: Array<{ __typename?: 'AvailableWorkspaceOutput', id: string, displayName?: string | null, subdomain: string, logo?: string | null, sso: Array<{ __typename?: 'SSOConnection', type: IdentityProviderType, id: string, issuer: string, name: string, status: SsoIdentityProviderStatus }> }> } | { __typename: 'UserNotExists', exists: boolean } };export type GetPublicWorkspaceDataBySubdomainQueryVariables = Exact<{ [key: string]: never; }>;@@ -3069,6 +3070,7 @@ export const CheckUserExistsDocument = gql`__typename... on UserExists {exists+ defaultWorkspaceIdavailableWorkspaces {iddisplayNamediff --git a/packages/twenty-front/src/modules/auth/graphql/queries/checkUserExists.ts b/packages/twenty-front/src/modules/auth/graphql/queries/checkUserExists.tsindex 0a3c9b0aca4..9f886cbda76 100644--- a/packages/twenty-front/src/modules/auth/graphql/queries/checkUserExists.ts+++ b/packages/twenty-front/src/modules/auth/graphql/queries/checkUserExists.ts@@ -6,6 +6,7 @@ export const CHECK_USER_EXISTS = gql`__typename... on UserExists {exists+ defaultWorkspaceIdavailableWorkspaces {iddisplayNamediff --git a/packages/twenty-front/src/modules/auth/hooks/useAuth.ts b/packages/twenty-front/src/modules/auth/hooks/useAuth.tsindex 510f3f66c72..c9dd1968bab 100644--- a/packages/twenty-front/src/modules/auth/hooks/useAuth.ts+++ b/packages/twenty-front/src/modules/auth/hooks/useAuth.ts@@ -136,8 +136,10 @@ export const useAuth = () => {await client.clearStore();sessionStorage.clear();localStorage.clear();+ // We need to explicitly clear the state to trigger the cookie deletion which include the parent domain+ setLastAuthenticateWorkspaceDomain(null);},- [client, goToRecoilSnapshot],+ [client, goToRecoilSnapshot, setLastAuthenticateWorkspaceDomain],);const handleChallenge = useCallback(diff --git a/packages/twenty-front/src/modules/auth/sign-in-up/components/SignInUpGlobalScopeForm.tsx b/packages/twenty-front/src/modules/auth/sign-in-up/components/SignInUpGlobalScopeForm.tsxindex 870754bab9c..c38e45ebedb 100644--- a/packages/twenty-front/src/modules/auth/sign-in-up/components/SignInUpGlobalScopeForm.tsx+++ b/packages/twenty-front/src/modules/auth/sign-in-up/components/SignInUpGlobalScopeForm.tsx@@ -83,21 +83,19 @@ export const SignInUpGlobalScopeForm = () => {},onCompleted: (data) => {requestFreshCaptchaToken();- if (data.checkUserExists.__typename === 'UserExists') {- if (- isDefined(data?.checkUserExists.availableWorkspaces) &&- data.checkUserExists.availableWorkspaces.length >= 1- ) {- return redirectToWorkspaceDomain(- data?.checkUserExists.availableWorkspaces[0].subdomain,- pathname,- {- email: form.getValues('email'),- },- );+ const response = data.checkUserExists;+ if (response.__typename === 'UserExists') {+ if (response.availableWorkspaces.length >= 1) {+ const workspace =+ response.availableWorkspaces.find(+ (workspace) => workspace.id === response.defaultWorkspaceId,+ ) ?? response.availableWorkspaces[0];+ return redirectToWorkspaceDomain(workspace.subdomain, pathname, {+ email: form.getValues('email'),+ });}}- if (data.checkUserExists.__typename === 'UserNotExists') {+ if (response.__typename === 'UserNotExists') {setSignInUpMode(SignInUpMode.SignUp);setSignInUpStep(SignInUpStep.Password);}diff --git a/packages/twenty-server/src/engine/core-modules/auth/dto/user-exists.entity.ts b/packages/twenty-server/src/engine/core-modules/auth/dto/user-exists.entity.tsindex e8f7f9a3e2e..db49594fe06 100644--- a/packages/twenty-server/src/engine/core-modules/auth/dto/user-exists.entity.ts+++ b/packages/twenty-server/src/engine/core-modules/auth/dto/user-exists.entity.ts@@ -7,6 +7,9 @@ export class UserExists {@Field(() => Boolean)exists: true;+ @Field(() => String)+ defaultWorkspaceId: string;+@Field(() => [AvailableWorkspaceOutput])availableWorkspaces: Array<AvailableWorkspaceOutput>;}diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/auth.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/auth.service.tsindex f96ae829be3..0adc408f35a 100644--- a/packages/twenty-server/src/engine/core-modules/auth/services/auth.service.ts+++ b/packages/twenty-server/src/engine/core-modules/auth/services/auth.service.ts@@ -257,6 +257,7 @@ export class AuthService {if (userValidator.isDefined(user)) {return {exists: true,+ defaultWorkspaceId: user.defaultWorkspaceId,availableWorkspaces: await this.findAvailableWorkspacesByEmail(email),};}diff --git a/packages/twenty-server/src/main.ts b/packages/twenty-server/src/main.tsindex 28cce925ded..c15387c59a5 100644--- a/packages/twenty-server/src/main.ts+++ b/packages/twenty-server/src/main.ts@@ -38,7 +38,7 @@ const bootstrap = async () => {const logger = app.get(LoggerService);const environmentService = app.get(EnvironmentService);- // TODO: Double check this as it's not working for now, it's going to be heplful for durable trees in twenty "orm"+ // TODO: Double check this as it's not working for now, it's going to be helpful for durable trees in twenty "orm"// // Apply context id strategy for durable trees// ContextIdFactory.apply(new AggregateByWorkspaceContextIdStrategy());