diff --git a/package-lock.json b/package-lock.json index fa0d51c..6f37f3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gc-shared-resources", - "version": "1.0.6", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "gc-shared-resources", - "version": "1.0.6", + "version": "1.1.0", "license": "MIT", "dependencies": { "@nuxt/kit": "^3.13.1" diff --git a/package.json b/package.json index ae7744c..bfc74cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gc-shared-resources", - "version": "1.0.6", + "version": "1.1.0", "description": "Shared resources for Guardian Connector Nuxt.js applications", "type": "module", "scripts": { diff --git a/src/composables/useAuth.ts b/src/composables/useAuth.ts deleted file mode 100644 index 3bee3b4..0000000 --- a/src/composables/useAuth.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { ref, onMounted } from "vue"; -import { useRouter } from "#imports"; - -export const useAuth = () => { - const errorMessage = ref(""); - const redirectPath = ref(""); - // @ts-ignore to avoid type error on useUserSession, which will be available at runtime in the app - const { loggedIn } = useUserSession(); - const router = useRouter(); - // @ts-ignore to avoid type error on useLocalePath, which will be available at runtime in the app - const localePath = useLocalePath(); - - onMounted(() => { - const redirect = router.currentRoute.value.query.redirect; - redirectPath.value = redirect - ? decodeURIComponent(redirect as string) - : localePath("/"); - - const urlParams = new URLSearchParams(window.location.search); - const code = urlParams.get("code"); - - if (code) { - window.location.href = `/auth/auth0?code=${code}`; - } - - const error = urlParams.get("error"); - const errorDescription = urlParams.get("error_description"); - - if (error === "access_denied") { - errorMessage.value = decodeURIComponent(errorDescription || ""); - } - - if (loggedIn.value) { - router.push(redirectPath.value); - } - }); - - return { - errorMessage, - loggedIn, - redirectPath, - }; -}; diff --git a/src/imports.d.ts b/src/imports.d.ts new file mode 100644 index 0000000..04fb61b --- /dev/null +++ b/src/imports.d.ts @@ -0,0 +1,7 @@ +import * as Imports from "#imports"; + +declare module "#imports" { + export * from "#imports"; + export function useUserSession(): { loggedIn: { value: boolean } }; + export function useLocalePath(): (path: string) => string; +} diff --git a/src/module.ts b/src/module.ts index 8d1682e..8ccba75 100644 --- a/src/module.ts +++ b/src/module.ts @@ -19,7 +19,7 @@ export default defineNuxtModule({ const { resolve } = createResolver(import.meta.url); // Add composables directory - addImportsDir(resolve("./composables")); + addImportsDir(resolve("./runtime/composables")); // Add components directory addComponentsDir({ diff --git a/src/runtime/composables/useAuth.ts b/src/runtime/composables/useAuth.ts new file mode 100644 index 0000000..6cb918a --- /dev/null +++ b/src/runtime/composables/useAuth.ts @@ -0,0 +1,36 @@ +import { ref } from "vue"; +import { useRouter, useLocalePath } from "#imports"; + +export const useAuth = (loggedIn: { value: boolean }) => { + let errorMessage; + const redirectPath = ref(""); + + const router = useRouter(); + // @ts-ignore to avoid type error on useLocalePath, which will be available at runtime in the app + const localePath = useLocalePath(); + + const redirect = router.currentRoute.value.query.redirect; + redirectPath.value = redirect + ? decodeURIComponent(redirect as string) + : localePath("/"); + + const urlParams = new URLSearchParams(window.location.search); + const code = urlParams.get("code"); + + if (code) { + window.location.href = `/auth/auth0?code=${code}`; + } + + const error = urlParams.get("error"); + const errorDescription = urlParams.get("error_description"); + + if (error === "access_denied") { + errorMessage = decodeURIComponent(errorDescription || ""); + } + + if (loggedIn.value) { + router.push(redirectPath.value); + } + + return errorMessage; +}; diff --git a/src/runtime/middleware/oauth.global.ts b/src/runtime/middleware/oauth.global.ts index cbb8edc..0639bbf 100644 --- a/src/runtime/middleware/oauth.global.ts +++ b/src/runtime/middleware/oauth.global.ts @@ -1,6 +1,7 @@ import { defineNuxtRouteMiddleware, useRuntimeConfig, + useUserSession, navigateTo, } from "#imports"; diff --git a/src/runtime/server/middleware/apiAuth.ts b/src/runtime/server/middleware/apiAuth.ts index 94c216e..f3d65a9 100644 --- a/src/runtime/server/middleware/apiAuth.ts +++ b/src/runtime/server/middleware/apiAuth.ts @@ -1,31 +1,26 @@ -import { defineEventHandler, H3Event, createError, eventHandler } from "h3"; +import { H3Event, createError, eventHandler } from "h3"; +import { useRuntimeConfig } from "#imports"; -export default (appApiKey: string) => - defineEventHandler( - eventHandler((event: H3Event) => { - const url = event.node.req.url; - if (!url) { - return; - } +export default eventHandler((event) => { + const { + public: { appApiKey }, + } = useRuntimeConfig(); + const url = event.node.req.url; - // Only apply middleware to API routes - if (!url.startsWith("/api/")) { - return; - } - - // Bypass middleware for specific paths - if (url.startsWith("/api/map") || url.startsWith("/api/_auth/")) { - return; - } - - // Match the API key from the request with the app's API key - const requestApiKey = event.node.req.headers["x-api-key"]; - - if (requestApiKey !== appApiKey) { - throw createError({ - status: 403, - message: "Forbidden", - }); - } - }), - ); + if (!url) { + return; + } + if (!url.startsWith("/api/")) { + return; + } + if (url.startsWith("/api/map") || url.startsWith("/api/_auth/")) { + return; + } + const requestApiKey = event.node.req.headers["x-api-key"]; + if (requestApiKey !== appApiKey) { + throw createError({ + status: 403, + message: "Forbidden", + }); + } +});