Skip to content

Commit ceb83a3

Browse files
authored
remove accessToken from being sent to client components (#206)
* remove accessToken from being sent to client components * ignore unused vars in sanitize function * improve type on sanitize helper function * remove oauthTokens from mock in test
1 parent 0d76d0c commit ceb83a3

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

__tests__/authkit-provider.spec.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ describe('useAuth', () => {
219219
permissions: ['read', 'write'],
220220
entitlements: ['feature1'],
221221
impersonator: { email: 'admin@example.com' },
222-
oauthTokens: { access_token: 'token123' },
223-
accessToken: 'access123',
224222
});
225223

226224
const TestComponent = () => {

src/actions.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
'use server';
22

33
import { signOut } from './auth.js';
4+
import { NoUserInfo, UserInfo } from './interfaces.js';
45
import { refreshSession, withAuth } from './session.js';
56
import { getWorkOS } from './workos.js';
67

8+
/**
9+
* This function is used to sanitize the auth object.
10+
* Remove the accessToken from the auth object as it is not needed on the client side.
11+
* @param value - The auth object to sanitize
12+
* @returns The sanitized auth object
13+
*/
14+
function sanitize<T extends UserInfo | NoUserInfo>(value: T) {
15+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
16+
const { accessToken, ...sanitized } = value;
17+
return sanitized;
18+
}
19+
720
/**
821
* This action is only accessible to authenticated users,
922
* there is no need to check the session here as the middleware will
@@ -22,7 +35,7 @@ export const getOrganizationAction = async (organizationId: string) => {
2235
};
2336

2437
export const getAuthAction = async (options?: { ensureSignedIn?: boolean }) => {
25-
return await withAuth(options);
38+
return sanitize(await withAuth(options));
2639
};
2740

2841
export const refreshAuthAction = async ({
@@ -32,5 +45,5 @@ export const refreshAuthAction = async ({
3245
ensureSignedIn?: boolean;
3346
organizationId?: string;
3447
}) => {
35-
return await refreshSession({ ensureSignedIn, organizationId });
48+
return sanitize(await refreshSession({ ensureSignedIn, organizationId }));
3649
};

src/components/authkit-provider.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type AuthContextType = {
1212
permissions: string[] | undefined;
1313
entitlements: string[] | undefined;
1414
impersonator: Impersonator | undefined;
15-
accessToken: string | undefined;
1615
loading: boolean;
1716
getAuth: (options?: { ensureSignedIn?: boolean }) => Promise<void>;
1817
refreshAuth: (options?: { ensureSignedIn?: boolean; organizationId?: string }) => Promise<void | { error: string }>;
@@ -38,7 +37,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
3837
const [permissions, setPermissions] = useState<string[] | undefined>(undefined);
3938
const [entitlements, setEntitlements] = useState<string[] | undefined>(undefined);
4039
const [impersonator, setImpersonator] = useState<Impersonator | undefined>(undefined);
41-
const [accessToken, setAccessToken] = useState<string | undefined>(undefined);
4240
const [loading, setLoading] = useState(true);
4341

4442
const getAuth = async ({ ensureSignedIn = false }: { ensureSignedIn?: boolean } = {}) => {
@@ -51,7 +49,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
5149
setPermissions(auth.permissions);
5250
setEntitlements(auth.entitlements);
5351
setImpersonator(auth.impersonator);
54-
setAccessToken(auth.accessToken);
5552
} catch (error) {
5653
setUser(null);
5754
setSessionId(undefined);
@@ -60,7 +57,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
6057
setPermissions(undefined);
6158
setEntitlements(undefined);
6259
setImpersonator(undefined);
63-
setAccessToken(undefined);
6460
} finally {
6561
setLoading(false);
6662
}
@@ -81,7 +77,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
8177
setPermissions(auth.permissions);
8278
setEntitlements(auth.entitlements);
8379
setImpersonator(auth.impersonator);
84-
setAccessToken(auth.accessToken);
8580
} catch (error) {
8681
return error instanceof Error ? { error: error.message } : { error: String(error) };
8782
} finally {
@@ -154,7 +149,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
154149
permissions,
155150
entitlements,
156151
impersonator,
157-
accessToken,
158152
loading,
159153
getAuth,
160154
refreshAuth,

0 commit comments

Comments
 (0)