From 8b01175b477f9ba1316915f0f1c8d96f6b6e7c4b Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Thu, 17 Aug 2023 13:29:31 +0530 Subject: [PATCH] blitz-auth: `useAuthenticatedBlitzContext` returns `AuthenticatedCtx` (#4198) Co-authored-by: Brandon Bayer --- .changeset/fresh-camels-return.md | 5 +++++ packages/blitz-auth/src/server/auth-sessions.ts | 4 +++- packages/blitz-auth/src/shared/types.ts | 2 +- packages/blitz-rpc/src/server/resolvers/resolver.ts | 4 ++-- packages/blitz/src/types.ts | 4 ++-- 5 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 .changeset/fresh-camels-return.md diff --git a/.changeset/fresh-camels-return.md b/.changeset/fresh-camels-return.md new file mode 100644 index 0000000000..2023ad897e --- /dev/null +++ b/.changeset/fresh-camels-return.md @@ -0,0 +1,5 @@ +--- +"@blitzjs/auth": patch +--- + +Updated `useAuthenticatedBlitzContext` to now return `AuthenticatedCtx` diff --git a/packages/blitz-auth/src/server/auth-sessions.ts b/packages/blitz-auth/src/server/auth-sessions.ts index d3f3fbfd51..3d2a06985f 100644 --- a/packages/blitz-auth/src/server/auth-sessions.ts +++ b/packages/blitz-auth/src/server/auth-sessions.ts @@ -15,6 +15,7 @@ import { log, baseLogger, chalk, + AuthenticatedCtx, } from "blitz" import { EmptyPublicData, @@ -219,7 +220,7 @@ export async function useAuthenticatedBlitzContext({ redirectTo?: string | RouteUrlObject redirectAuthenticatedTo?: string | RouteUrlObject | ((ctx: Ctx) => string | RouteUrlObject) role?: string | string[] -}): Promise { +}): Promise { const log = baseLogger().getSubLogger({name: "useAuthenticatedBlitzContext"}) const customChalk = new chalk.Instance({ level: log.settings.type === "json" ? 0 : chalk.level, @@ -268,6 +269,7 @@ export async function useAuthenticatedBlitzContext({ redirect(redirectTo) } } + return ctx as AuthenticatedCtx } const makeProxyToPublicData = (ctxClass: T): T => { diff --git a/packages/blitz-auth/src/shared/types.ts b/packages/blitz-auth/src/shared/types.ts index 1625814030..1018c37e65 100644 --- a/packages/blitz-auth/src/shared/types.ts +++ b/packages/blitz-auth/src/shared/types.ts @@ -82,7 +82,7 @@ declare module "blitz" { export interface Ctx { session: SessionContext } - export interface AuthenticatedMiddlewareCtx extends Omit { + export interface AuthenticatedCtx extends Omit { session: AuthenticatedSessionContext } } diff --git a/packages/blitz-rpc/src/server/resolvers/resolver.ts b/packages/blitz-rpc/src/server/resolvers/resolver.ts index c2775f0cd9..4184437884 100644 --- a/packages/blitz-rpc/src/server/resolvers/resolver.ts +++ b/packages/blitz-rpc/src/server/resolvers/resolver.ts @@ -1,5 +1,5 @@ import type {SessionContext, SessionContextBase} from "@blitzjs/auth" -import {Await, Ctx, EnsurePromise, AuthenticatedMiddlewareCtx} from "blitz" +import {Await, Ctx, EnsurePromise, AuthenticatedCtx} from "blitz" import type {input as zInput, output as zOutput, ZodTypeAny} from "zod" export type ParserType = "sync" | "async" @@ -271,7 +271,7 @@ interface ResolverAuthorize { (...args: Parameters): ( input: T, ctx: C, - ) => ResultWithContext + ) => ResultWithContext } const authorize: ResolverAuthorize = (...args) => { diff --git a/packages/blitz/src/types.ts b/packages/blitz/src/types.ts index f9f25ab932..0b086553d1 100644 --- a/packages/blitz/src/types.ts +++ b/packages/blitz/src/types.ts @@ -2,7 +2,7 @@ import {UrlObject} from "url" // Context for plugins to declaration merge stuff into export interface Ctx {} -export interface AuthenticatedMiddlewareCtx {} +export interface AuthenticatedCtx {} export type CodegenField = { component: string @@ -17,7 +17,7 @@ export interface RouteUrlObject extends Pick