File tree Expand file tree Collapse file tree 4 files changed +20
-20
lines changed
Expand file tree Collapse file tree 4 files changed +20
-20
lines changed Original file line number Diff line number Diff line change 11import { createClerkClient , type User } from "@clerk/backend" ;
2- import { TRPCError } from "@trpc/server" ;
3- import { t } from "@worker/trpc" ;
42import type { Context } from "../context" ;
53
64export type AuthenticatedContext = Context & { user : User } ;
75
8- export const authentication = t . middleware ( async ( { next, ctx } ) => {
9- try {
10- const user = await authFunction ( ctx ) ;
11- return next ( { ctx : { ...ctx , user } } ) ;
12- } catch ( error ) {
13- console . error ( error ) ;
14- throw new TRPCError ( { message : "Unable to get auth" , cause : error , code : "UNAUTHORIZED" } ) ;
15- }
16- } ) ;
17-
186export async function authFunction ( ctx : Context ) {
197 const clerk = createClerkClient ( { secretKey : ctx . env . CLERK_SECRET_KEY , publishableKey : ctx . env . CLERK_PUBLISHABLE_KEY } ) ;
208 const requestState = await clerk . authenticateRequest ( ctx . req ) ;
Original file line number Diff line number Diff line change 1+ import { TRPCError } from "@trpc/server" ;
2+ import { t } from "@worker/trpc" ;
3+ import { authFunction } from "@worker/trpc/middlewares/auth" ;
4+ import { rateLimitFunction } from "@worker/trpc/middlewares/rate-limit" ;
5+
6+ export const authentication = t . middleware ( async ( { next, ctx } ) => {
7+ try {
8+ const user = await authFunction ( ctx ) ;
9+ return next ( { ctx : { ...ctx , user } } ) ;
10+ } catch ( error ) {
11+ console . error ( error ) ;
12+ throw new TRPCError ( { message : "Unable to get auth" , cause : error , code : "UNAUTHORIZED" } ) ;
13+ }
14+ } ) ;
15+
16+ export const rateLimit = t . middleware ( async ( { next, ctx } ) => {
17+ await rateLimitFunction ( ctx )
18+ return next ( { ctx } ) ;
19+ } ) ;
Original file line number Diff line number Diff line change 11import { TRPCError } from "@trpc/server" ;
22import { Ratelimit } from "@upstash/ratelimit" ;
3- import { t } from "@worker/trpc" ;
43import type { Context } from "@worker/trpc/context" ;
54import { isLocal } from "@worker/trpc/util/local" ;
65
@@ -32,8 +31,3 @@ export async function rateLimitFunction(ctx: Context) {
3231 } ) ;
3332 }
3433}
35-
36- export const rateLimit = t . middleware ( async ( { next, ctx } ) => {
37- await rateLimitFunction ( ctx )
38- return next ( { ctx } ) ;
39- } ) ;
Original file line number Diff line number Diff line change 11import { t } from "@worker/trpc" ;
2- import { authentication } from "@worker/trpc/middlewares/auth" ;
3- import { rateLimit } from "@worker/trpc/middlewares/rate-limit" ;
2+ import { rateLimit , authentication } from "@worker/trpc/middlewares" ;
43
54export const publicProcedure = t . procedure . use ( rateLimit ) ;
65export const privateProcedure = t . procedure . use ( rateLimit ) . use ( authentication ) ;
You can’t perform that action at this time.
0 commit comments