Skip to content

Commit 3182402

Browse files
modify getCredential signature and get the user after redirect handle for web (#1327)
1 parent 1d296dd commit 3182402

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/hooks/Auth0Context.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,17 @@ export interface Auth0ContextInterface extends AuthState {
6767
* Retrieves the stored credentials, refreshing them if necessary.
6868
* @param scope The scopes to request for the new access token (used during refresh).
6969
* @param minTtl The minimum time-to-live (in seconds) required for the access token.
70+
* @param parameters Additional parameters to send during the refresh request.
71+
* @param forceRefresh If true, forces a refresh of the credentials.
7072
* @returns A promise that resolves with the user's credentials.
7173
* @throws {AuthError} If credentials cannot be retrieved or refreshed.
7274
*/
73-
getCredentials(scope?: string, minTtl?: number): Promise<Credentials>;
75+
getCredentials(
76+
scope?: string,
77+
minTtl?: number,
78+
parameters?: Record<string, unknown>,
79+
forceRefresh?: boolean
80+
): Promise<Credentials>;
7481

7582
/**
7683
* Clears the user's credentials without clearing their web session and logs them out.

src/hooks/Auth0Provider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ export const Auth0Provider = ({
5555
window?.location?.search?.includes('state=');
5656
if (hasRedirectParams) {
5757
try {
58-
user = await client.webAuth.getWebUser();
5958
// If it does, handle the redirect. This will exchange the code for tokens.
6059
await client.webAuth.handleRedirectCallback();
60+
// should get the user after handle redirect
61+
user = await client.webAuth.getWebUser();
6162
// Clean the URL
6263
window.history.replaceState(
6364
{},

src/hooks/__tests__/Auth0Provider.spec.tsx

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import '@testing-library/jest-dom';
1010
import { Auth0Provider, useAuth0 } from '..';
1111
import Auth0 from '../../index';
12-
import { Auth0User } from '../../core/models';
1312

1413
// Mock TurboModuleRegistry first
1514
jest.mock('react-native/Libraries/TurboModule/TurboModuleRegistry', () => ({
@@ -68,8 +67,12 @@ jest.mock('../../index');
6867
const MockAuth0 = Auth0 as jest.MockedClass<typeof Auth0>;
6968

7069
// Mock the Auth0User model's factory method
71-
jest.mock('../../core/models/Auth0User');
72-
const MockAuth0User = Auth0User as jest.MockedClass<typeof Auth0User>;
70+
jest.mock('../../core/models/Auth0User', () => ({
71+
Auth0User: {
72+
fromIdToken: jest.fn(),
73+
},
74+
}));
75+
const { Auth0User: MockAuth0User } = require('../../core/models/Auth0User');
7376

7477
// 2. A more complete mock client factory
7578
const createMockClient = () => {
@@ -89,6 +92,7 @@ const createMockClient = () => {
8992
cancelWebAuth: jest.fn().mockResolvedValue(undefined),
9093
handleRedirectCallback: jest.fn().mockResolvedValue(undefined),
9194
checkWebSession: jest.fn().mockResolvedValue(null),
95+
getWebUser: jest.fn().mockResolvedValue(null),
9296
},
9397
credentialsManager: {
9498
hasValidCredentials: jest.fn().mockResolvedValue(false),
@@ -803,4 +807,70 @@ describe('Auth0Provider', () => {
803807
});
804808
});
805809
});
810+
811+
// Web Platform Method Tests
812+
describe('Web Platform Methods', () => {
813+
it('should verify webAuth methods exist and are callable', () => {
814+
// Verify all required webAuth methods exist in the mock
815+
expect(mockClientInstance.webAuth.handleRedirectCallback).toBeDefined();
816+
expect(mockClientInstance.webAuth.getWebUser).toBeDefined();
817+
expect(mockClientInstance.webAuth.checkWebSession).toBeDefined();
818+
819+
// Verify they are functions
820+
expect(typeof mockClientInstance.webAuth.handleRedirectCallback).toBe(
821+
'function'
822+
);
823+
expect(typeof mockClientInstance.webAuth.getWebUser).toBe('function');
824+
expect(typeof mockClientInstance.webAuth.checkWebSession).toBe(
825+
'function'
826+
);
827+
});
828+
829+
it('should verify webAuth methods can be mocked properly', async () => {
830+
const mockUser = { sub: 'test|123', name: 'Test User' };
831+
832+
// Setup mocks
833+
mockClientInstance.webAuth.handleRedirectCallback.mockResolvedValue(
834+
undefined
835+
);
836+
mockClientInstance.webAuth.getWebUser.mockResolvedValue(mockUser);
837+
mockClientInstance.webAuth.checkWebSession.mockResolvedValue(undefined);
838+
839+
// Call the methods
840+
await mockClientInstance.webAuth.handleRedirectCallback();
841+
const user = await mockClientInstance.webAuth.getWebUser();
842+
await mockClientInstance.webAuth.checkWebSession();
843+
844+
// Verify calls were made
845+
expect(
846+
mockClientInstance.webAuth.handleRedirectCallback
847+
).toHaveBeenCalledTimes(1);
848+
expect(mockClientInstance.webAuth.getWebUser).toHaveBeenCalledTimes(1);
849+
expect(mockClientInstance.webAuth.checkWebSession).toHaveBeenCalledTimes(
850+
1
851+
);
852+
expect(user).toEqual(mockUser);
853+
});
854+
855+
it('should verify the sequence of webAuth method calls can be tracked', async () => {
856+
// Setup mocks
857+
mockClientInstance.webAuth.handleRedirectCallback.mockResolvedValue(
858+
undefined
859+
);
860+
mockClientInstance.webAuth.getWebUser.mockResolvedValue(null);
861+
862+
// Call methods in sequence
863+
await mockClientInstance.webAuth.handleRedirectCallback();
864+
await mockClientInstance.webAuth.getWebUser();
865+
866+
// Verify call order using invocationCallOrder
867+
const handleRedirectCallOrder =
868+
mockClientInstance.webAuth.handleRedirectCallback.mock
869+
.invocationCallOrder[0];
870+
const getWebUserCallOrder =
871+
mockClientInstance.webAuth.getWebUser.mock.invocationCallOrder[0];
872+
873+
expect(getWebUserCallOrder).toBeGreaterThan(handleRedirectCallOrder);
874+
});
875+
});
806876
});

0 commit comments

Comments
 (0)