Skip to content

Commit

Permalink
feat(middleware): improve middleware for checking token expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Aug 16, 2024
1 parent 6241955 commit d53920f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 16 deletions.
4 changes: 2 additions & 2 deletions components/analysis/AnalysesTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getAnalysisNodes } from "~/composables/useAPIFetch";
import { formatDataRow } from "~/utils/format-data-row";
import TableRowMetadata from "~/components/TableRowMetadata.vue";
import ExpandRowButtons from "~/components/table/ExpandRowButtons.vue";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
import { showHubAdapterConnectionErrorToast } from "~/composables/connectionErrorToast";
const expandedRows = ref();
const analyses = ref();
Expand All @@ -25,7 +25,7 @@ if (status.value === "success") {
expandRowEntries,
);
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
showHubAdapterConnectionErrorToast();
}
function onToggleRowExpansion(rowIds) {
Expand Down
2 changes: 0 additions & 2 deletions components/data-stores/DataStoreListTabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { formatDataRow } from "~/utils/format-data-row";
import type { Analysis, DetailedService, Project, Route } from "~/services/Api";
import DetailedDataStoreTable from "~/components/data-stores/tables/DetailedDataStoreTable.vue";
import DetailedAnalysisTable from "~/components/data-stores/tables/DetailedAnalysisTable.vue";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
import DataStoreTreeTable from "~/components/data-stores/tables/DataStoreTreeTable.vue";
const dataStores = ref();
Expand Down Expand Up @@ -76,7 +75,6 @@ async function loadDetailedDataStoreTable(responseData, status, error) {
});
dataStores.value = formattedDataStores;
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
dataStores.value = [];
}
loading.value = false;
Expand Down
4 changes: 2 additions & 2 deletions components/projects/ProjectTable.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { getProjects } from "~/composables/useAPIFetch";
import { formatDataRow } from "~/utils/format-data-row";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
import { showHubAdapterConnectionErrorToast } from "~/composables/connectionErrorToast";
const projects = ref();
Expand All @@ -16,7 +16,7 @@ if (status.value === "success") {
expandRowEntries,
);
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
showHubAdapterConnectionErrorToast();
}
</script>

Expand Down
4 changes: 2 additions & 2 deletions components/projects/ProposalTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { formatDataRow } from "~/utils/format-data-row";
import TableRowMetadata from "~/components/TableRowMetadata.vue";
import type { ProjectNode } from "~/services/Api";
import ExpandRowButtons from "~/components/table/ExpandRowButtons.vue";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
import { showHubAdapterConnectionErrorToast } from "~/composables/connectionErrorToast";
const proposals = ref();
const expandedRows = ref({});
Expand All @@ -21,7 +21,7 @@ if (status.value === "success") {
expandRowEntries,
);
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
showHubAdapterConnectionErrorToast();
}
function onToggleRowExpansion(rowIds) {
Expand Down
14 changes: 13 additions & 1 deletion composables/connectionErrorToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ export const useToastService = () => {
return getToast();
};

export const showConnectionErrorToast = () => {
export const showHubAdapterConnectionErrorToast = () => {
const toast = useToastService();
toast.add({
severity: "error",
summary: "Connection error",
detail: "Unable to contact the API.",
life: 3000,
});
console.warn("Hub Adapter API service unreachable");
};

export const showKongConnectionErrorToast = () => {
const toast = useToastService();
toast.add({
severity: "error",
summary: "Connection error",
detail: "Unable to contact the Kong gateway service.",
life: 3000,
});
console.warn("Kong service unreachable");
};
7 changes: 7 additions & 0 deletions composables/useAPIFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ export function updateAnalysis(analysis_id: string, updates) {

// Kong endpoints
export function getDataStores(includeProject: boolean, opts?) {
// return nuxtApp.$hubApi("/kong/datastore", {
// ...opts,
// method: "GET",
// query: {
// detailed: includeProject,
// },
// });
return useAPIFetch<{ data: ListServices }>("/kong/datastore", {
...opts,
method: "GET",
Expand Down
8 changes: 7 additions & 1 deletion middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export default defineNuxtRouteMiddleware(async (to) => {
const { loggedIn, login } = useOidcAuth();
const { loggedIn, login, user } = useOidcAuth();
if (!loggedIn.value && to.path !== "/") {
console.warn("Not logged in");
return login();
}

if (Date.now() > Date.parse(user?.value.expireAt.toString())) {
console.warn("Token expired, routing to IDP");
await login();
navigateTo(to.path);
}
});
3 changes: 2 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export default defineNuxtConfig({
userinfoUrl:
process.env.KEYCLOAK_SERVICE_URL +
"/protocol/openid-connect/userinfo",
pkce: true,
},
},
session: {
expirationCheck: false,
automaticRefresh: true,
automaticRefresh: false,
maxAge: 3600,
},
middleware: {
Expand Down
23 changes: 18 additions & 5 deletions plugins/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {
showKongConnectionErrorToast,
showHubAdapterConnectionErrorToast,
} from "~/composables/connectionErrorToast";

export default defineNuxtPlugin((nuxtApp) => {
const { user } = useOidcAuth();
const { user, login } = useOidcAuth();
const config = useRuntimeConfig();
const baseUrl = config.public.hubAdapterUrl as string;
const hubApi = $fetch.create({
Expand All @@ -15,12 +20,20 @@ export default defineNuxtPlugin((nuxtApp) => {
onRequestError({ error }) {
console.log(error);
},
onResponseError({ response }) {
onResponseError({ request, response }) {
// Handle the response errors
console.log(response);
if (response.status === 401 || response.status === 403) {
console.warn("User signed out, routing to login");
nuxtApp.runWithContext(() => navigateTo("/auth/login"));
// if (response.status === 401 || response.status === 403) {
// console.warn("User not signed in, routing to login");
// return login();
// }

if (response.status === 500) {
if (typeof request === "string" && request.includes("kong")) {
showKongConnectionErrorToast();
} else {
showHubAdapterConnectionErrorToast();
}
}
},
});
Expand Down

0 comments on commit d53920f

Please sign in to comment.