diff --git a/src/network/__tests__/useExchangeCodeForToken.test.tsx b/src/network/__tests__/useExchangeCodeForToken.test.tsx index af21ae0..9d4c8fb 100644 --- a/src/network/__tests__/useExchangeCodeForToken.test.tsx +++ b/src/network/__tests__/useExchangeCodeForToken.test.tsx @@ -5,7 +5,7 @@ import { AuthProvider } from 'src/utils/AuthContext' import { asMock, currentAuthCredentials, userIsNotSignedIn, userIsSignedIn } from 'src/testHelpers' import { faker } from '@faker-js/faker' import { signInWithCode } from '../auth' -import { currentUrlSearchParams } from 'src/utils/currentUrlSearchParams' +import { currentUrlSearchParamsFor } from 'src/utils/currentUrlSearchParamsFor' import { navigate } from 'gatsby' jest.mock('src/network/auth', () => { @@ -14,9 +14,9 @@ jest.mock('src/network/auth', () => { } }) -jest.mock('src/utils/currentUrlSearchParams', () => { +jest.mock('src/utils/currentUrlSearchParamsFor', () => { return { - currentUrlSearchParams: jest.fn(), + currentUrlSearchParamsFor: jest.fn(), } }) @@ -56,9 +56,9 @@ describe('useExchangeCodeForToken', () => { ExpiresIn: faker.number.int({ min: 1000, max: 4000 }), }, }) - asMock(currentUrlSearchParams).mockReturnValue(new URLSearchParams(`code=${code}`)) + asMock(currentUrlSearchParamsFor).mockReturnValue(code) asMock(navigate).mockImplementation(async () => { - asMock(currentUrlSearchParams).mockReturnValue(new URLSearchParams()) + asMock(currentUrlSearchParamsFor).mockReturnValue(null) }) }) @@ -86,9 +86,7 @@ describe('useExchangeCodeForToken', () => { kind: 'NOT_AUTHORIZED', error: { name: 'Not authorized', message: 'not authorized' }, }) - asMock(currentUrlSearchParams).mockReturnValue( - new URLSearchParams(`code=${faker.lorem.word()}`), - ) + asMock(currentUrlSearchParamsFor).mockReturnValue(faker.lorem.word()) }) it('displays the error', async () => { @@ -106,9 +104,7 @@ describe('useExchangeCodeForToken', () => { beforeEach(() => { asMock(navigate).mockClear() userIsSignedIn() - asMock(currentUrlSearchParams).mockReturnValue( - new URLSearchParams(`code=${faker.lorem.word()}`), - ) + asMock(currentUrlSearchParamsFor).mockReturnValue(faker.lorem.word()) }) it('does not try to sign in', async () => { diff --git a/src/network/useExchangeCodeForToken.ts b/src/network/useExchangeCodeForToken.ts index da9f734..1b2b281 100644 --- a/src/network/useExchangeCodeForToken.ts +++ b/src/network/useExchangeCodeForToken.ts @@ -1,11 +1,11 @@ import { useEffect, useState } from 'react' import { useAuth } from 'src/utils/AuthContext' -import { currentUrlSearchParams } from 'src/utils/currentUrlSearchParams' +import { currentUrlSearchParamsFor } from 'src/utils/currentUrlSearchParamsFor' import { signInWithCode } from './auth' import { navigate } from 'gatsby' export const useExchangeCodeForToken = () => { - const code: string | null = currentUrlSearchParams().get('code') + const code: string | null = currentUrlSearchParamsFor('code') const [loading, setLoading] = useState(false) const [auth, setAuth] = useAuth() const [errorMessage, setErrorMessage] = useState('') diff --git a/src/utils/currentUrlSearchParams.ts b/src/utils/currentUrlSearchParams.ts deleted file mode 100644 index 2c10c61..0000000 --- a/src/utils/currentUrlSearchParams.ts +++ /dev/null @@ -1 +0,0 @@ -export const currentUrlSearchParams = () => new URLSearchParams(window.location.search) diff --git a/src/utils/currentUrlSearchParamsFor.ts b/src/utils/currentUrlSearchParamsFor.ts new file mode 100644 index 0000000..d32aa9e --- /dev/null +++ b/src/utils/currentUrlSearchParamsFor.ts @@ -0,0 +1,3 @@ +export const currentUrlSearchParamsFor = (key: string) => { + return typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get(key) : null +} \ No newline at end of file