From af87d87c9163959e9dea081a370ee5ada7cfcec0 Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Wed, 18 May 2022 09:33:11 -0700 Subject: [PATCH] update auth recipe --- recipes/authorization.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/recipes/authorization.md b/recipes/authorization.md index 2bbf89c..7bd5acf 100644 --- a/recipes/authorization.md +++ b/recipes/authorization.md @@ -6,7 +6,7 @@ The `createContext`-function is called for each incoming request so here you can ```ts // ~/server/trpc/index.ts -import type * as trpc from '@trpc/server' +import type { inferAsyncReturnType } from '@trpc/server' import type { CompatibilityEvent } from 'h3' import { decodeAndVerifyJwtToken } from '~/somewhere/in/your/app/utils' @@ -30,7 +30,7 @@ export async function createContext({ req }: CompatibilityEvent) { } } -type Context = trpc.inferAsyncReturnType +type Context = inferAsyncReturnType // [..] Define API handler and app router ``` @@ -38,6 +38,8 @@ type Context = trpc.inferAsyncReturnType ### Option 1: Authorize using resolver ```ts +import { TRPCError } from '@trpc/server' + export const router = trpc .router() // open for anyone @@ -51,7 +53,7 @@ export const router = trpc .query('secret', { resolve: ({ ctx }) => { if (!ctx.user) - throw new trpc.TRPCError({ code: 'UNAUTHORIZED' }) + throw new TRPCError({ code: 'UNAUTHORIZED' }) return { secret: 'sauce', @@ -65,7 +67,6 @@ export const router = trpc ```ts import * as trpc from '@trpc/server' import { TRPCError } from '@trpc/server' -import { createRouter } from '../createRouter' export const router = trpc .router()