Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/utils/workos.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import httpStatus from "http-status";
import { AUTH_MESSAGES } from "constants/messages/auth.constant";
import type { SessionCookieData, User as WorkOSUser } from "@workos-inc/node";
import { APP_LOGGER } from "shared/logger";
import { roleRepository, userRepository } from "database/repositories";
import { RoleType } from "types/role.types";

const IS_DEVELOPMENT = env.NODE_ENV === "development";

Expand Down Expand Up @@ -128,6 +130,17 @@ export const setWorkOSUserContext = async (
const workOSRole = organizationMemberships.data[0]?.role.slug;
const user = await syncWorkOSUser(workOSUser);

// Sync role from WorkOS if it differs from the database role
if (workOSRole && user.role?.name !== workOSRole) {
const matchedRole = await roleRepository.findOneBy({
name: workOSRole as RoleType,
});
if (matchedRole) {
await userRepository.update(user.id, { role: matchedRole });
user.role = matchedRole;
}
}

res.locals.workosUser = workOSUser;
res.locals.userRole = workOSRole;
res.locals.user = user;
Expand Down
Loading