Skip to content

Commit

Permalink
remove accessToken from being sent to client components (#206)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nicknisi authored Feb 21, 2025
1 parent 0d76d0c commit ceb83a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 0 additions & 2 deletions __tests__/authkit-provider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,6 @@ describe('useAuth', () => {
permissions: ['read', 'write'],
entitlements: ['feature1'],
impersonator: { email: '[email protected]' },
oauthTokens: { access_token: 'token123' },
accessToken: 'access123',
});

const TestComponent = () => {
Expand Down
17 changes: 15 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
'use server';

import { signOut } from './auth.js';
import { NoUserInfo, UserInfo } from './interfaces.js';
import { refreshSession, withAuth } from './session.js';
import { getWorkOS } from './workos.js';

/**
* This function is used to sanitize the auth object.
* Remove the accessToken from the auth object as it is not needed on the client side.
* @param value - The auth object to sanitize
* @returns The sanitized auth object
*/
function sanitize<T extends UserInfo | NoUserInfo>(value: T) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { accessToken, ...sanitized } = value;
return sanitized;
}

/**
* This action is only accessible to authenticated users,
* there is no need to check the session here as the middleware will
Expand All @@ -22,7 +35,7 @@ export const getOrganizationAction = async (organizationId: string) => {
};

export const getAuthAction = async (options?: { ensureSignedIn?: boolean }) => {
return await withAuth(options);
return sanitize(await withAuth(options));
};

export const refreshAuthAction = async ({
Expand All @@ -32,5 +45,5 @@ export const refreshAuthAction = async ({
ensureSignedIn?: boolean;
organizationId?: string;
}) => {
return await refreshSession({ ensureSignedIn, organizationId });
return sanitize(await refreshSession({ ensureSignedIn, organizationId }));
};
6 changes: 0 additions & 6 deletions src/components/authkit-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type AuthContextType = {
permissions: string[] | undefined;
entitlements: string[] | undefined;
impersonator: Impersonator | undefined;
accessToken: string | undefined;
loading: boolean;
getAuth: (options?: { ensureSignedIn?: boolean }) => Promise<void>;
refreshAuth: (options?: { ensureSignedIn?: boolean; organizationId?: string }) => Promise<void | { error: string }>;
Expand All @@ -38,7 +37,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
const [permissions, setPermissions] = useState<string[] | undefined>(undefined);
const [entitlements, setEntitlements] = useState<string[] | undefined>(undefined);
const [impersonator, setImpersonator] = useState<Impersonator | undefined>(undefined);
const [accessToken, setAccessToken] = useState<string | undefined>(undefined);
const [loading, setLoading] = useState(true);

const getAuth = async ({ ensureSignedIn = false }: { ensureSignedIn?: boolean } = {}) => {
Expand All @@ -51,7 +49,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
setPermissions(auth.permissions);
setEntitlements(auth.entitlements);
setImpersonator(auth.impersonator);
setAccessToken(auth.accessToken);
} catch (error) {
setUser(null);
setSessionId(undefined);
Expand All @@ -60,7 +57,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
setPermissions(undefined);
setEntitlements(undefined);
setImpersonator(undefined);
setAccessToken(undefined);
} finally {
setLoading(false);
}
Expand All @@ -81,7 +77,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
setPermissions(auth.permissions);
setEntitlements(auth.entitlements);
setImpersonator(auth.impersonator);
setAccessToken(auth.accessToken);
} catch (error) {
return error instanceof Error ? { error: error.message } : { error: String(error) };
} finally {
Expand Down Expand Up @@ -154,7 +149,6 @@ export const AuthKitProvider = ({ children, onSessionExpired }: AuthKitProviderP
permissions,
entitlements,
impersonator,
accessToken,
loading,
getAuth,
refreshAuth,
Expand Down

0 comments on commit ceb83a3

Please sign in to comment.