Skip to content

Commit

Permalink
Client auth fixes (#777)
Browse files Browse the repository at this point in the history
* add redirct on unauthorized interceptor and return null in loader

* increment versions
  • Loading branch information
dpgraham4401 authored Aug 22, 2024
1 parent 3517505 commit 802318e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/app/features/layout/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const rootLoader: LoaderFunction = async () => {

return query
.unwrap()
.catch((_err) => console.error('Error fetching orgs'))
.catch((_err) => null)
.finally(() => query.unsubscribe());
};

Expand Down
5 changes: 2 additions & 3 deletions client/app/features/siteDetails/SiteDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactElement } from 'react';
import { LoaderFunction, redirect, useNavigate, useParams } from 'react-router-dom';
import { LoaderFunction, useNavigate, useParams } from 'react-router-dom';
import { RcraSiteDetails } from '~/components/RcraSite/RcraSiteDetails';
import { Button, Card, CardContent, CardHeader, Spinner } from '~/components/ui';
import { rootStore as store, useGetUserHaztrakSiteQuery } from '~/store';
Expand All @@ -13,8 +13,7 @@ export const siteDetailsLoader: LoaderFunction = async ({ params }) => {
try {
return await p.unwrap();
} catch (_error) {
console.error('Error fetching orgs');
return redirect('/login');
return null;
} finally {
p.unsubscribe();
}
Expand Down
25 changes: 25 additions & 0 deletions client/app/services/APIs/htApi.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it, vi } from 'vitest';
import { AxiosResponse } from 'axios';
import { returnOnSuccess } from '~/services/APIs/htApi';
import { undefined } from 'zod';

vi.mock('react-router-dom', () => ({
redirect: vi.fn(),
}));

const createMockResponse = (status: number): AxiosResponse => ({
// @ts-expect-error - ok for test
config: {},
data: undefined,
headers: {},
status,
statusText: '',
});

describe('htApi response interceptor', () => {
it('returns same response', async () => {
const response = createMockResponse(200);
const returnedResponse = returnOnSuccess(response);
expect(returnedResponse).toBe(response);
});
});
14 changes: 13 additions & 1 deletion client/app/services/APIs/htApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**htApi.ts - service for making requests to the Haztrak API*/
import axios, { InternalAxiosRequestConfig } from 'axios';
import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import { rootStore } from '~/store';
import { redirect } from 'react-router-dom';

/** An Axios instance with an interceptor to automatically apply authentication headers*/
export const htApi = axios.create({
Expand Down Expand Up @@ -28,3 +29,14 @@ htApi.interceptors.request.use(
return Promise.reject(error);
}
);

export const returnOnSuccess = (response: AxiosResponse) => response;

export const redirectOnUnauthorized = (error: AxiosError) => {
if (error.response?.status === 401) {
redirect('/login');
}
return Promise.reject(error);
};

htApi.interceptors.response.use(returnOnSuccess, redirectOnUnauthorized);
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haztrak",
"version": "0.7.2",
"version": "0.8.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion server/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "haztrak"
version = "0.7.1"
version = "0.8.0"
description = "An open-source web app illustrating how waste management software can interface with RCRAInfo to track hazardous waste"
readme = "README.md"
authors = [
Expand Down

0 comments on commit 802318e

Please sign in to comment.