Skip to content

Commit

Permalink
fix: propagated the error query string solution to the remaining loca…
Browse files Browse the repository at this point in the history
…tions it was missing
  • Loading branch information
meza committed Jul 8, 2023
1 parent c2e9319 commit 49a6c3c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Auth0 Remix Server > getting the user > when the access token is valid > and the user profile fetch fails > redirects to the failed url 1`] = `"/logout?error=test-error"`;

exports[`Auth0 Remix Server > getting the user > when the token is expired > and there is no other loader refreshing the token > and there is a refresh token > redirects to the failed login url when the refresh fails 1`] = `"/logout?error=test-error2"`;

exports[`Auth0 Remix Server > getting the user > when there are no credentials returned > redirects to the failed login url 1`] = `"/logout?error=no_credentials"`;

exports[`Auth0 Remix Server > handling the callback token exchange > when there is a code in the exchange as a GET > redirects to the failed login url if the token exchange fails 2`] = `
{
"body": "grant_type=authorization_code&client_id=clientId&client_secret=clientSecret&code=test-code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth0%2Fcallback",
Expand Down
16 changes: 14 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@ describe('Auth0 Remix Server', () => {

const authorizer = new Auth0RemixServer(authOptions);
await expect(authorizer.getUser(request, context)).rejects.toThrowError(redirectError); // a redirect happened
const redirectUrl = vi.mocked(redirect).mock.calls[0][0];
expect(redirectUrl).toMatchSnapshot();
expect(consoleSpy).toHaveBeenCalledWith('No credentials found');
});
});
Expand Down Expand Up @@ -796,7 +798,9 @@ describe('Auth0 Remix Server', () => {
} as never;

vi.mocked(fetch).mockResolvedValue({
ok: false
ok: false,
status: 400,
json: () => Promise.resolve({ error: 'test-error' })
} as never);

const consoleSpy = vi.spyOn(console, 'error').mockImplementation(noop);
Expand All @@ -806,6 +810,9 @@ describe('Auth0 Remix Server', () => {
const authorizer = new Auth0RemixServer(authOptions);
await expect(authorizer.getUser(request, {})).rejects.toThrowError(redirectError); // a redirect happened
expect(consoleSpy).toHaveBeenCalledWith('Failed to get user profile from Auth0');
const redirectUrl = vi.mocked(redirect).mock.calls[0][0];
expect(redirectUrl).toMatchSnapshot();
expect(redirectUrl).toContain('error=test-error');
});
});
});
Expand Down Expand Up @@ -859,14 +866,19 @@ describe('Auth0 Remix Server', () => {

it<LocalTestContext>('redirects to the failed login url when the refresh fails', async ({ authOptions, appLoadContext }) => {
vi.mocked(fetch).mockResolvedValue({
ok: false
ok: false,
status: 400,
json: () => Promise.resolve({ error: 'test-error2' })
} as never);
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(noop);
const request = new Request('https://it-doesnt-matter.com');

const authorizer = new Auth0RemixServer(authOptions);
await expect(authorizer.getUser(request, appLoadContext)).rejects.toThrowError(redirectError); // a redirect happened
expect(consoleSpy).toHaveBeenCalledWith('Failed to refresh token from Auth0');
const redirectUrl = vi.mocked(redirect).mock.calls[0][0];
expect(redirectUrl).toMatchSnapshot();
expect(redirectUrl).toContain('error=test-error2');
});

it<LocalTestContext>('returns the correct credentials with the rotation off', async ({ authOptions, appLoadContext }) => {
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class Auth0RemixServer {
credentials = await getCredentials(request, this.session);
} catch (err) {
console.error('No credentials found');
throw redirect(this.failedLoginRedirect);
throw redirect(this.failedLoginRedirect + '?error=no_credentials');
}

try {
Expand Down Expand Up @@ -288,10 +288,12 @@ export class Auth0RemixServer {
method: 'POST',
body: body.toString()
});
const searchParams = new URLSearchParams();

if (!response.ok) {
console.error('Failed to refresh token from Auth0');
throw redirect(this.failedLoginRedirect);
searchParams.set('error', await this.getErrorReason(response));
throw redirect(this.failedLoginRedirect.concat('?', searchParams.toString()));
}
const data = (await response.json()) as Auth0Credentials;
const userData: UserCredentials = {
Expand All @@ -317,9 +319,12 @@ export class Auth0RemixServer {
}
});

const searchParams = new URLSearchParams();

if (!response.ok) {
console.error('Failed to get user profile from Auth0');
throw redirect(this.failedLoginRedirect);
searchParams.set('error', await this.getErrorReason(response));
throw redirect(this.failedLoginRedirect.concat('?', searchParams.toString()));
}

const data = (await response.json()) as Auth0UserProfile;
Expand Down

0 comments on commit 49a6c3c

Please sign in to comment.