Skip to content

Commit

Permalink
fix(nextjs): Deprecate user, session, organization from auth() (#1960)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Oct 30, 2023
1 parent fb89c9a commit 1a95065
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-rings-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Deprecate `user`, `session`, and `organization` resources from the returned value of `auth()`.
40 changes: 37 additions & 3 deletions packages/nextjs/src/server/getAuth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Organization, Session, SignedInAuthObject, SignedOutAuthObject, User } from '@clerk/backend';
import type { AuthObject, Organization, Session, SignedInAuthObject, SignedOutAuthObject, User } from '@clerk/backend';
import {
AuthStatus,
constants,
Expand All @@ -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';
Expand All @@ -18,6 +19,21 @@ import { getAuthKeyFromRequest, getCookie, getHeader, injectSSRStateIntoObject }

type GetAuthOpts = Partial<SecretKeyOrApiKey>;

type AuthObjectWithDeprecatedResources<T extends AuthObject> = Omit<T, 'user' | 'organization' | 'session'> & {
/**
* @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,
noAuthStatusMessage,
Expand All @@ -26,7 +42,10 @@ export const createGetAuth = ({
debugLoggerName: string;
}) =>
withLogger(debugLoggerName, logger => {
return (req: RequestLike, opts?: GetAuthOpts): SignedInAuthObject | SignedOutAuthObject => {
return (
req: RequestLike,
opts?: GetAuthOpts,
): AuthObjectWithDeprecatedResources<SignedInAuthObject | SignedOutAuthObject> => {
const debug = getHeader(req, constants.Headers.EnableDebug) === 'true';
if (debug) {
logger.enable();
Expand Down Expand Up @@ -61,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;
};
});

Expand Down

0 comments on commit 1a95065

Please sign in to comment.