From e675c8734c4cbe5a6fc2077e194a5d2ad8264555 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Mon, 30 Oct 2023 09:57:27 -0700 Subject: [PATCH] fix(nextjs): Deprecate user, session, organization from auth() (#1960) (cherry picked from commit 1a95065eba3809bfecd0a6584b6a1465d172b36c) --- .changeset/three-rings-wash.md | 5 ++++ packages/nextjs/src/server/getAuth.ts | 35 ++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 .changeset/three-rings-wash.md diff --git a/.changeset/three-rings-wash.md b/.changeset/three-rings-wash.md new file mode 100644 index 0000000000..59ce3fbdea --- /dev/null +++ b/.changeset/three-rings-wash.md @@ -0,0 +1,5 @@ +--- +'@clerk/nextjs': patch +--- + +Deprecate `user`, `session`, and `organization` resources from the returned value of `auth()`. diff --git a/packages/nextjs/src/server/getAuth.ts b/packages/nextjs/src/server/getAuth.ts index cf37b83897..a93e1ae5ea 100644 --- a/packages/nextjs/src/server/getAuth.ts +++ b/packages/nextjs/src/server/getAuth.ts @@ -8,6 +8,7 @@ import { signedInAuthObject, signedOutAuthObject, } from '@clerk/backend'; +import { deprecatedObjectProperty } from '@clerk/shared'; import type { SecretKeyOrApiKey } from '@clerk/types'; import { withLogger } from '../utils/debugLogger'; @@ -18,7 +19,20 @@ import { getAuthKeyFromRequest, getCookie, getHeader, injectSSRStateIntoObject } type GetAuthOpts = Partial; -type AuthObjectWithoutResources = Omit; +type AuthObjectWithDeprecatedResources = Omit & { + /** + * @deprecated This will be removed in the next major version + */ + user: T['user']; + /** + * @deprecated This will be removed in the next major version + */ + organization: T['organization']; + /** + * @deprecated This will be removed in the next major version + */ + session: T['session']; +}; export const createGetAuth = ({ debugLoggerName, @@ -31,7 +45,7 @@ export const createGetAuth = ({ return ( req: RequestLike, opts?: GetAuthOpts, - ): AuthObjectWithoutResources => { + ): AuthObjectWithDeprecatedResources => { const debug = getHeader(req, constants.Headers.EnableDebug) === 'true'; if (debug) { logger.enable(); @@ -66,7 +80,22 @@ export const createGetAuth = ({ const jwt = parseJwt(req); logger.debug('JWT debug', jwt.raw.text); - return signedInAuthObject(jwt.payload, { ...options, token: jwt.raw.text }); + + const signedIn = signedInAuthObject(jwt.payload, { ...options, token: jwt.raw.text }); + + if (signedIn.user) { + deprecatedObjectProperty(signedIn, 'user', 'Use `clerkClient.users.getUser` instead.'); + } + + if (signedIn.organization) { + deprecatedObjectProperty(signedIn, 'organization', 'Use `clerkClient.organizations.getOrganization` instead.'); + } + + if (signedIn.session) { + deprecatedObjectProperty(signedIn, 'session', 'Use `clerkClient.sessions.getSession` instead.'); + } + + return signedIn; }; });