Skip to content

Commit

Permalink
fix: fix isUser middleware will call twice problem
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Nov 3, 2024
1 parent 6a4bdd3 commit c70e698
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"scripts": {
"dev": "tsx watch --env-file=.env ./main.ts",
"dev:debug": "tsx --inspect-brk --env-file=.env ./main.ts",
"build": "tsc",
"postinstall": "pnpm db:generate",
"check:type": "tsc --noEmit --skipLibCheck",
Expand Down
7 changes: 4 additions & 3 deletions src/server/trpc/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,23 @@ const isSystemAdmin = isUser.unstable_pipe(async (opts) => {
});

export const systemAdminProcedure = t.procedure.use(prom).use(isSystemAdmin);
export const workspaceProcedure = protectProedure
export const workspaceProcedure = publicProcedure
.input(
z.object({
workspaceId: z.string().cuid2(),
})
)
.use(createWorkspacePermissionMiddleware());

export const workspaceAdminProcedure = protectProedure
export const workspaceAdminProcedure = publicProcedure
.input(
z.object({
workspaceId: z.string().cuid2(),
})
)
.use(createWorkspacePermissionMiddleware([ROLES.owner, ROLES.admin]));

export const workspaceOwnerProcedure = protectProedure
export const workspaceOwnerProcedure = publicProcedure
.input(
z.object({
workspaceId: z.string().cuid2(),
Expand All @@ -145,6 +145,7 @@ export const workspaceOwnerProcedure = protectProedure

/**
* Create a trpc middleware which help user check workspace permission
* NOTE: this middleware already include user auth, so we dont need use it under protectProedure which will trigger user auth twice.
*/
function createWorkspacePermissionMiddleware(roles: ROLES[] = []) {
return isUser.unstable_pipe(async (opts) => {
Expand Down

0 comments on commit c70e698

Please sign in to comment.