Skip to content

Commit 1fdb91f

Browse files
committed
cleanup
1 parent ca3dab7 commit 1fdb91f

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

packages/auth/src/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ export const auth = betterAuth({
272272
],
273273
});
274274

275+
export const websitesApi = {
276+
hasPermission: auth.api.hasPermission,
277+
};
278+
275279
export type User = (typeof auth)['$Infer']['Session']['user'] & {
276280
role?: string;
277281
};

packages/rpc/src/routers/websites.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { websitesApi } from '@databuddy/auth';
12
import { and, chQuery, eq, isNull, websites } from '@databuddy/db';
23
import { createDrizzleCache, redis } from '@databuddy/redis';
34
import { TRPCError } from '@trpc/server';
@@ -88,7 +89,7 @@ export const websitesRouter = createTRPCRouter({
8889
);
8990
return ctx.db.query.websites.findMany({
9091
where,
91-
orderBy: (websites, { desc }) => [desc(websites.createdAt)],
92+
orderBy: (table, { desc }) => [desc(table.createdAt)],
9293
});
9394
},
9495
});
@@ -102,15 +103,15 @@ export const websitesRouter = createTRPCRouter({
102103
key: cacheKey,
103104
ttl: 60,
104105
tables: ['websites'],
105-
queryFn: async () => authorizeWebsiteAccess(ctx, input.id, 'read'),
106+
queryFn: () => authorizeWebsiteAccess(ctx, input.id, 'read'),
106107
});
107108
}),
108109

109110
create: protectedProcedure
110111
.input(createWebsiteSchema)
111112
.mutation(async ({ ctx, input }) => {
112113
if (input.organizationId) {
113-
const { success } = await ctx.auth.api.hasPermission({
114+
const { success } = await websitesApi.hasPermission({
114115
headers: ctx.headers,
115116
body: { permissions: { website: ['create'] } },
116117
});
@@ -254,14 +255,10 @@ export const websitesRouter = createTRPCRouter({
254255
transfer: protectedProcedure
255256
.input(transferWebsiteSchema)
256257
.mutation(async ({ ctx, input }) => {
257-
const website = await authorizeWebsiteAccess(
258-
ctx,
259-
input.websiteId,
260-
'update'
261-
);
258+
await authorizeWebsiteAccess(ctx, input.websiteId, 'update');
262259

263260
if (input.organizationId) {
264-
const { success } = await ctx.auth.api.hasPermission({
261+
const { success } = await websitesApi.hasPermission({
265262
headers: ctx.headers,
266263
body: { permissions: { website: ['create'] } },
267264
});

packages/rpc/src/utils/auth.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { websitesApi } from '@databuddy/auth';
12
import { db, eq, websites } from '@databuddy/db';
23
import { cacheable } from '@databuddy/redis';
34
import { TRPCError } from '@trpc/server';
@@ -8,7 +9,9 @@ type Permission = 'read' | 'update' | 'delete' | 'transfer';
89
const getWebsiteById = cacheable(
910
async (id: string) => {
1011
try {
11-
if (!id) return null;
12+
if (!id) {
13+
return null;
14+
}
1215
return await db.query.websites.findFirst({
1316
where: eq(websites.id, id),
1417
});
@@ -59,7 +62,7 @@ export async function authorizeWebsiteAccess(
5962
}
6063

6164
if (website.organizationId) {
62-
const { success } = await ctx.auth.api.hasPermission({
65+
const { success } = await websitesApi.hasPermission({
6366
headers: ctx.headers,
6467
body: { permissions: { website: [permission] } },
6568
});

0 commit comments

Comments
 (0)