Skip to content

Commit

Permalink
Make workspaceMemberId optional in JWT for workspaces that are not AC…
Browse files Browse the repository at this point in the history
…TIVE (twentyhq#6714)

WorkspaceMemberId is mandatory in the jwt token generated for a given
user on a given workspace.
However, when a user signs up, it does not have a workspaceMemberId yet.
  • Loading branch information
charlesBochet authored Aug 21, 2024
1 parent da4bd73 commit eab202f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ import {
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { JwtWrapperService } from 'src/engine/core-modules/jwt/services/jwt-wrapper.service';
import { User } from 'src/engine/core-modules/user/user.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import {
Workspace,
WorkspaceActivationStatus,
} from 'src/engine/core-modules/workspace/workspace.entity';
import { EmailService } from 'src/engine/integrations/email/email.service';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
Expand Down Expand Up @@ -94,33 +97,39 @@ export class TokenService {
);
}

const workspaceIdNonNullable = workspaceId
? workspaceId
: user.defaultWorkspace.id;
const tokenWorkspaceId = workspaceId ?? user.defaultWorkspace.id;
let tokenWorkspaceMemberId: string | undefined;

const workspaceMemberRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkspaceMemberWorkspaceEntity>(
workspaceIdNonNullable,
'workspaceMember',
);
if (
user.defaultWorkspace.activationStatus ===
WorkspaceActivationStatus.ACTIVE
) {
const workspaceMemberRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkspaceMemberWorkspaceEntity>(
tokenWorkspaceId,
'workspaceMember',
);

const workspaceMember = await workspaceMemberRepository.findOne({
where: {
userId: user.id,
},
});
const workspaceMember = await workspaceMemberRepository.findOne({
where: {
userId: user.id,
},
});

if (!workspaceMember) {
throw new AuthException(
'User is not a member of the workspace',
AuthExceptionCode.FORBIDDEN_EXCEPTION,
);
if (!workspaceMember) {
throw new AuthException(
'User is not a member of the workspace',
AuthExceptionCode.FORBIDDEN_EXCEPTION,
);
}

tokenWorkspaceMemberId = workspaceMember.id;
}

const jwtPayload: JwtPayload = {
sub: user.id,
workspaceId: workspaceId ? workspaceId : user.defaultWorkspace.id,
workspaceMemberId: workspaceMember.id,
workspaceMemberId: tokenWorkspaceMemberId,
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ApiKeyWorkspaceEntity } from 'src/modules/api-key/standard-objects/api-
export type JwtPayload = {
sub: string;
workspaceId: string;
workspaceMemberId: string;
workspaceMemberId?: string;
jti?: string;
};

Expand Down

0 comments on commit eab202f

Please sign in to comment.