mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2026-02-02 18:21:37 +08:00
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
// Src/server/router/context.ts
|
|
import type { Session } from 'next-auth';
|
|
import * as trpc from '@trpc/server';
|
|
import type * as trpcNext from '@trpc/server/adapters/next';
|
|
|
|
import { getServerAuthSession } from '~/server/common/get-server-auth-session';
|
|
import { prisma } from '~/server/db/client';
|
|
|
|
type CreateContextOptions = {
|
|
session: Session | null;
|
|
};
|
|
|
|
/** Use this helper for:
|
|
* - testing, where we dont have to Mock Next.js' req/res
|
|
* - trpc's `createSSGHelpers` where we don't have req/res
|
|
**/
|
|
export const createContextInner = async (opts: CreateContextOptions) => {
|
|
return {
|
|
prisma,
|
|
session: opts.session,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* This is the actual context you'll use in your router
|
|
* @link https://trpc.io/docs/context
|
|
**/
|
|
export const createContext = async (
|
|
opts: trpcNext.CreateNextContextOptions,
|
|
) => {
|
|
const { req, res } = opts;
|
|
|
|
// Get the session from the server using the unstable_getServerSession wrapper function
|
|
const session = await getServerAuthSession({ req, res });
|
|
|
|
return await createContextInner({
|
|
session,
|
|
});
|
|
};
|
|
|
|
type Context = trpc.inferAsyncReturnType<typeof createContext>;
|
|
|
|
export const createRouter = () => trpc.router<Context>();
|
|
|
|
/**
|
|
* Creates a tRPC router that asserts all queries and mutations are from an authorized user. Will throw an unauthorized error if a user is not signed in.
|
|
**/
|
|
export function createProtectedRouter() {
|
|
return createRouter().middleware(({ ctx, next }) => {
|
|
if (!ctx.session || !ctx.session.user) {
|
|
throw new trpc.TRPCError({ code: 'UNAUTHORIZED' });
|
|
}
|
|
return next({
|
|
ctx: {
|
|
...ctx,
|
|
// Infers that `session` is non-nullable to downstream resolvers
|
|
session: { ...ctx.session, user: ctx.session.user },
|
|
},
|
|
});
|
|
});
|
|
}
|