diff --git a/ui/src/app/auth/authContext.tsx b/ui/src/app/auth/authContext.tsx index c354d6e..2e0a4a2 100644 --- a/ui/src/app/auth/authContext.tsx +++ b/ui/src/app/auth/authContext.tsx @@ -1,7 +1,6 @@ "use client" import { createContext, useContext, useState, useEffect } from 'react'; -import { Dispatch, SetStateAction } from 'react'; import { User } from '../types'; import { useCookies } from 'react-cookie'; import { jwtDecode } from 'jwt-decode'; @@ -24,6 +23,7 @@ export const AuthProvider = ({ children }: Readonly<{ children: React.ReactNode let userObject = jwtDecode(cookies.user_token) as User setUser(userObject); } else { + setUser(null) router.push('/login'); } }, [cookies.user_token, router]); diff --git a/ui/src/app/certificate_requests/page.tsx b/ui/src/app/certificate_requests/page.tsx index b0a4f6b..0e61848 100644 --- a/ui/src/app/certificate_requests/page.tsx +++ b/ui/src/app/certificate_requests/page.tsx @@ -42,7 +42,12 @@ export default function CertificateRequests() { queryFn: () => getCertificateRequests({ authToken: cookies.user_token }) }) if (query.status == "loading") { return } - if (query.status == "error") { return } + if (query.status == "error") { + if (query.error.message.includes("401")) { + removeCookie("user_token") + } + return + } const csrs = Array.from(query.data ? query.data : []) return ( diff --git a/ui/src/app/queries.ts b/ui/src/app/queries.ts index 0bb1bdd..b0f32a2 100644 --- a/ui/src/app/queries.ts +++ b/ui/src/app/queries.ts @@ -1,4 +1,5 @@ import { CSREntry } from "./types" +import { HTTPStatus } from "./utils" export type RequiredParams = { id: string @@ -12,12 +13,12 @@ export async function getCertificateRequests(params: { authToken: string }): Pro headers: { "Authorization": "Bearer " + params.authToken } }) if (!response.ok) { - throw new Error('Network response was not ok') + throw new Error(`${response.status} ${HTTPStatus(response.status)}`) } return response.json() } -export async function postCSR(params: {authToken: string, csr: string}) { +export async function postCSR(params: { authToken: string, csr: string }) { if (!params.csr) { throw new Error('CSR not provided') } @@ -30,7 +31,7 @@ export async function postCSR(params: {authToken: string, csr: string}) { body: params.csr.trim() }) if (!response.ok) { - throw new Error('Network response was not ok') + throw new Error(`${response.status} ${HTTPStatus(response.status)}`) } return response.json() } @@ -48,7 +49,7 @@ export async function postCertToID(params: RequiredParams) { body: params.cert.trim() }) if (!response.ok) { - throw new Error('Network response was not ok') + throw new Error(`${response.status} ${HTTPStatus(response.status)}`) } return response.json() } @@ -61,7 +62,7 @@ export async function deleteCSR(params: RequiredParams) { } }) if (!response.ok) { - throw new Error('Network response was not ok') + throw new Error(`${response.status} ${HTTPStatus(response.status)}`) } return response.json() } @@ -74,7 +75,7 @@ export async function rejectCSR(params: RequiredParams) { } }) if (!response.ok) { - throw new Error('Network response was not ok') + throw new Error(`${response.status} ${HTTPStatus(response.status)}`) } return response.json() } @@ -87,7 +88,7 @@ export async function revokeCertificate(params: RequiredParams) { } }) if (!response.ok) { - throw new Error('Network response was not ok') + throw new Error(`${response.status} ${HTTPStatus(response.status)}`) } return response.json() } @@ -97,9 +98,9 @@ export async function login(userForm: { username: string, password: string }) { method: "POST", body: JSON.stringify({ "username": userForm.username, "password": userForm.password }) }) + const responseText = await response.text() if (!response.ok) { - const responseText = await response.text() - throw new Error(responseText) + throw new Error(`${response.status} ${HTTPStatus(response.status)}. ${responseText}`) } - return response.text() + return responseText } \ No newline at end of file diff --git a/ui/src/app/utils.ts b/ui/src/app/utils.ts index 9750d0f..32e4ec1 100644 --- a/ui/src/app/utils.ts +++ b/ui/src/app/utils.ts @@ -1,4 +1,4 @@ -import { CertificationRequest, Certificate, Extension, Extensions, GeneralName, GeneralNames } from "pkijs"; +import { CertificationRequest, Certificate, Extensions } from "pkijs"; import { fromBER } from "asn1js"; import * as pvutils from "pvutils"; @@ -233,4 +233,14 @@ export const csrMatchesCertificate = (csrPemString: string, certPemString: strin const csrPKbytes = csr.subjectPublicKeyInfo.subjectPublicKey.valueBeforeDecodeView const certPKbytes = cert.subjectPublicKeyInfo.subjectPublicKey.valueBeforeDecodeView return csrPKbytes.toString() == certPKbytes.toString() +} + +export const HTTPStatus = (code: number): string => { + const map: { [key: number]: string } = { + 401: "Unauthorized", + } + if (!(code in map)) { + throw new Error("code not recognized: " + code) + } + return map[code] } \ No newline at end of file