(
+
+ {processing && done}
+ {state && state.from}
+
+ )}
+ />,
+ );
+
+ await waitForElement(() => getByTestId('done'));
+
+ expect(onAuthSuccess).toHaveBeenCalledTimes(1);
+ expect(onAuthError).not.toHaveBeenCalled();
+
+ expect(getByTestId('state')).toHaveTextContent('/success');
+ });
+
+ test('with custom token function and token uri fetch args', async () => {
+ fetch2.mockClear();
+
+ const props = {
+ tokenUrl: 'https://api.service.com/oauth2/token',
+ tokenFn: fetch2,
+ tokenFetchArgs: {
+ cache: 'no-cache',
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
+ },
+ clientId: 'abc',
+ clientSecret: 'abcdef',
+ redirectUri: 'https://www.test.com/redirect',
+ querystring: `?${qs.stringify({
+ code: 'abc',
+ state: JSON.stringify({ from: '/settings' }),
+ })}`,
+ };
+
+ const { getByTestId } = render(
+ (
+ {processing && done}
+ )}
+ />,
+ );
+
+ await waitForElement(() => getByTestId('done'));
+
expect(fetch2).toHaveBeenCalledWith(
expect.stringContaining(props.tokenUrl),
expect.objectContaining({
diff --git a/src/OauthReceiver/index.js b/src/OauthReceiver/index.js
index 6d18ad8..1d48bbe 100644
--- a/src/OauthReceiver/index.js
+++ b/src/OauthReceiver/index.js
@@ -32,6 +32,7 @@ class OauthReceiver extends React.Component {
clientSecret,
redirectUri,
args,
+ tokenFn,
onAuthSuccess,
} = this.props;
@@ -60,8 +61,10 @@ class OauthReceiver extends React.Component {
const defaultFetchArgs = { method: 'POST', headers };
const fetchArgs = Object.assign(defaultFetchArgs, tokenFetchArgs);
- fetch2(url, fetchArgs)
- .then(response => {
+ (typeof tokenFn === 'function' ?
+ tokenFn(url, fetchArgs) :
+ fetch2(url, fetchArgs)
+ ).then(response => {
const accessToken = response.access_token;
if (typeof onAuthSuccess === 'function') {
@@ -140,6 +143,7 @@ OauthReceiver.propTypes = {
),
location: PropTypes.shape({ search: PropTypes.string.isRequired }),
querystring: PropTypes.string,
+ tokenFn: PropTypes.func,
onAuthSuccess: PropTypes.func,
onAuthError: PropTypes.func,
render: PropTypes.func,
@@ -154,6 +158,7 @@ OauthReceiver.defaultProps = {
args: {},
location: null,
querystring: null,
+ tokenFn: null,
onAuthSuccess: null,
onAuthError: null,
render: null,