-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
182 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; | ||
|
||
import { logger } from '../../../../infrastructures/pino'; | ||
import { createContext } from '../../../../infrastructures/trpcBackend/context'; | ||
import { backendRouter } from '../../../../infrastructures/trpcBackend/routers'; | ||
|
||
// cf.https://trpc.io/docs/server/adapters/nextjs#route-handlers | ||
function handler(req: Request): Promise<Response> { | ||
return fetchRequestHandler({ | ||
createContext, | ||
endpoint: '/api/trpc', | ||
onError({ error }) { | ||
logger.error(error); | ||
}, | ||
req, | ||
router: backendRouter, | ||
}); | ||
} | ||
export { handler as GET, handler as POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use client'; | ||
|
||
import { createTRPCReact, httpBatchLink } from '@trpc/react-query'; | ||
|
||
import type { BackendRouter } from './routers'; | ||
|
||
// https://trpc.io/docs/client/react/setup | ||
export const backendTrpcReact = createTRPCReact<BackendRouter>(); | ||
|
||
export const backendTrpcReactClient = backendTrpcReact.createClient({ | ||
links: [httpBatchLink({ url: '/api/trpc' })], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { inferRouterContext } from '@trpc/server'; | ||
import type { FetchCreateContextFnOptions } from '@trpc/server/adapters/fetch'; | ||
|
||
import { logger } from '../pino'; | ||
|
||
import type { BackendRouter } from './routers'; | ||
|
||
export type Context = FetchCreateContextFnOptions; | ||
|
||
// cf. https://trpc.io/docs/server/adapters/fastify#create-the-context | ||
export function createContext(options: FetchCreateContextFnOptions): inferRouterContext<BackendRouter> { | ||
logger.debug(`%s %s` + (options.req.body ? `\n%o` : ''), options.req.method, options.req.url, options.req.body); | ||
|
||
return options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { TRPCError } from '@trpc/server'; | ||
|
||
import { getSessionOnNode } from '../../utils/sessionOnNode'; | ||
import { logger } from '../pino'; | ||
|
||
import { middleware } from './trpc'; | ||
|
||
const accessTokenRegex = /(^|;)\s*sAccessToken=([^;]*)/; | ||
|
||
// https://dev.to/franciscomendes10866/authentication-and-authorization-in-a-node-api-using-fastify-trpc-and-supertokens-3cgn | ||
export const authorize = middleware(async ({ ctx, next }) => { | ||
logger.trace('Headers: %o', ctx.req.headers); | ||
|
||
let session; | ||
try { | ||
const match = ctx.req.headers.get('Cookie')?.match(accessTokenRegex); | ||
const token = match && decodeURIComponent(match[2]); | ||
if (token) session = await getSessionOnNode(token); | ||
} catch (error) { | ||
logger.warn(error); | ||
} | ||
if (!session) { | ||
throw new TRPCError({ code: 'UNAUTHORIZED' }); | ||
} | ||
|
||
return next({ ctx: { session } }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod'; | ||
|
||
import { authorize } from '../middlewares'; | ||
import { procedure, router } from '../trpc'; | ||
|
||
export const backendRouter = router({ | ||
getSession: procedure | ||
.use(authorize) | ||
.output(z.object({ userId: z.string().uuid() })) | ||
.query(({ ctx }) => ({ userId: ctx.session.superTokensUserId })), | ||
}); | ||
|
||
// export type definition of API | ||
export type BackendRouter = typeof backendRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// cf. https://trpc.io/docs/client/nextjs/setup#3-create-a-trpc-router | ||
import { initTRPC } from '@trpc/server'; | ||
|
||
import type { Context } from './context'; | ||
|
||
const t = initTRPC.context<Context>().create(); | ||
|
||
export const middleware = t.middleware; | ||
export const router = t.router; | ||
export const procedure = t.procedure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server'; | ||
|
||
import type { BackendRouter } from './routers'; | ||
|
||
export type BackendRouterInputs = inferRouterInputs<BackendRouter>; | ||
export type BackendRouterOutputs = inferRouterOutputs<BackendRouter>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters