@@ -881,6 +881,103 @@ describe('Auth0Provider', () => {
881881 } ) ;
882882 } ) ;
883883
884+ it ( 'should provide a loginWithCustomTokenExchange method' , async ( ) => {
885+ const tokenResponse = {
886+ access_token : '__test_access_token__' ,
887+ id_token : '__test_id_token__' ,
888+ token_type : 'Bearer' ,
889+ expires_in : 86400 ,
890+ scope : 'openid profile email' ,
891+ } ;
892+ clientMock . loginWithCustomTokenExchange . mockResolvedValue ( tokenResponse ) ;
893+ const wrapper = createWrapper ( ) ;
894+ const { result } = renderHook (
895+ ( ) => useContext ( Auth0Context ) ,
896+ { wrapper }
897+ ) ;
898+ await waitFor ( ( ) => {
899+ expect ( result . current . loginWithCustomTokenExchange ) . toBeInstanceOf ( Function ) ;
900+ } ) ;
901+ let response ;
902+ await act ( async ( ) => {
903+ response = await result . current . loginWithCustomTokenExchange ( {
904+ subject_token : '__test_token__' ,
905+ subject_token_type : 'urn:test:token-type' ,
906+ scope : 'openid profile email' ,
907+ organization : 'org_123' ,
908+ } ) ;
909+ } ) ;
910+ expect ( clientMock . loginWithCustomTokenExchange ) . toHaveBeenCalledWith ( {
911+ subject_token : '__test_token__' ,
912+ subject_token_type : 'urn:test:token-type' ,
913+ scope : 'openid profile email' ,
914+ organization : 'org_123' ,
915+ } ) ;
916+ expect ( response ) . toStrictEqual ( tokenResponse ) ;
917+ } ) ;
918+
919+ it ( 'should handle errors when using loginWithCustomTokenExchange' , async ( ) => {
920+ clientMock . loginWithCustomTokenExchange . mockRejectedValue ( new Error ( '__test_error__' ) ) ;
921+ const wrapper = createWrapper ( ) ;
922+ const { result } = renderHook (
923+ ( ) => useContext ( Auth0Context ) ,
924+ { wrapper }
925+ ) ;
926+ await waitFor ( ( ) => {
927+ expect ( result . current . loginWithCustomTokenExchange ) . toBeInstanceOf ( Function ) ;
928+ } ) ;
929+ await act ( async ( ) => {
930+ await expect (
931+ result . current . loginWithCustomTokenExchange ( {
932+ subject_token : '__test_token__' ,
933+ subject_token_type : 'urn:test:token-type' ,
934+ } )
935+ ) . rejects . toThrow ( '__test_error__' ) ;
936+ } ) ;
937+ expect ( clientMock . loginWithCustomTokenExchange ) . toHaveBeenCalled ( ) ;
938+ } ) ;
939+
940+ it ( 'should update auth state after successful loginWithCustomTokenExchange' , async ( ) => {
941+ const user = { name : '__test_user__' } ;
942+ const tokenResponse = {
943+ access_token : '__test_access_token__' ,
944+ id_token : '__test_id_token__' ,
945+ token_type : 'Bearer' ,
946+ expires_in : 86400 ,
947+ } ;
948+ clientMock . loginWithCustomTokenExchange . mockResolvedValue ( tokenResponse ) ;
949+ clientMock . getUser . mockResolvedValue ( user ) ;
950+ const wrapper = createWrapper ( ) ;
951+ const { result } = renderHook (
952+ ( ) => useContext ( Auth0Context ) ,
953+ { wrapper }
954+ ) ;
955+ await waitFor ( ( ) => {
956+ expect ( result . current . loginWithCustomTokenExchange ) . toBeInstanceOf ( Function ) ;
957+ } ) ;
958+ await act ( async ( ) => {
959+ await result . current . loginWithCustomTokenExchange ( {
960+ subject_token : '__test_token__' ,
961+ subject_token_type : 'urn:test:token-type' ,
962+ } ) ;
963+ } ) ;
964+ expect ( clientMock . getUser ) . toHaveBeenCalled ( ) ;
965+ expect ( result . current . user ) . toStrictEqual ( user ) ;
966+ } ) ;
967+
968+ it ( 'should memoize the loginWithCustomTokenExchange method' , async ( ) => {
969+ const wrapper = createWrapper ( ) ;
970+ const { result, rerender } = renderHook (
971+ ( ) => useContext ( Auth0Context ) ,
972+ { wrapper }
973+ ) ;
974+ await waitFor ( ( ) => {
975+ const memoized = result . current . loginWithCustomTokenExchange ;
976+ rerender ( ) ;
977+ expect ( result . current . loginWithCustomTokenExchange ) . toBe ( memoized ) ;
978+ } ) ;
979+ } ) ;
980+
884981 it ( 'should provide an exchangeToken method' , async ( ) => {
885982 const tokenResponse = {
886983 access_token : '__test_access_token__' ,
@@ -889,7 +986,7 @@ describe('Auth0Provider', () => {
889986 expires_in : 86400 ,
890987 scope : 'openid profile email' ,
891988 } ;
892- clientMock . exchangeToken . mockResolvedValue ( tokenResponse ) ;
989+ clientMock . loginWithCustomTokenExchange . mockResolvedValue ( tokenResponse ) ;
893990 const wrapper = createWrapper ( ) ;
894991 const { result } = renderHook (
895992 ( ) => useContext ( Auth0Context ) ,
@@ -907,7 +1004,7 @@ describe('Auth0Provider', () => {
9071004 organization : 'org_123' ,
9081005 } ) ;
9091006 } ) ;
910- expect ( clientMock . exchangeToken ) . toHaveBeenCalledWith ( {
1007+ expect ( clientMock . loginWithCustomTokenExchange ) . toHaveBeenCalledWith ( {
9111008 subject_token : '__test_token__' ,
9121009 subject_token_type : 'urn:test:token-type' ,
9131010 scope : 'openid profile email' ,
@@ -916,8 +1013,8 @@ describe('Auth0Provider', () => {
9161013 expect ( response ) . toStrictEqual ( tokenResponse ) ;
9171014 } ) ;
9181015
919- it ( 'should handle errors when exchanging tokens' , async ( ) => {
920- clientMock . exchangeToken . mockRejectedValue ( new Error ( '__test_error__' ) ) ;
1016+ it ( 'should handle errors when exchanging tokens (deprecated method) ' , async ( ) => {
1017+ clientMock . loginWithCustomTokenExchange . mockRejectedValue ( new Error ( '__test_error__' ) ) ;
9211018 const wrapper = createWrapper ( ) ;
9221019 const { result } = renderHook (
9231020 ( ) => useContext ( Auth0Context ) ,
@@ -934,18 +1031,18 @@ describe('Auth0Provider', () => {
9341031 } )
9351032 ) . rejects . toThrow ( '__test_error__' ) ;
9361033 } ) ;
937- expect ( clientMock . exchangeToken ) . toHaveBeenCalled ( ) ;
1034+ expect ( clientMock . loginWithCustomTokenExchange ) . toHaveBeenCalled ( ) ;
9381035 } ) ;
9391036
940- it ( 'should update auth state after successful token exchange' , async ( ) => {
1037+ it ( 'should update auth state after successful token exchange (deprecated method) ' , async ( ) => {
9411038 const user = { name : '__test_user__' } ;
9421039 const tokenResponse = {
9431040 access_token : '__test_access_token__' ,
9441041 id_token : '__test_id_token__' ,
9451042 token_type : 'Bearer' ,
9461043 expires_in : 86400 ,
9471044 } ;
948- clientMock . exchangeToken . mockResolvedValue ( tokenResponse ) ;
1045+ clientMock . loginWithCustomTokenExchange . mockResolvedValue ( tokenResponse ) ;
9491046 clientMock . getUser . mockResolvedValue ( user ) ;
9501047 const wrapper = createWrapper ( ) ;
9511048 const { result } = renderHook (
0 commit comments