Skip to content

Commit

Permalink
Fix all runtime code for usage with built Nuxt projects
Browse files Browse the repository at this point in the history
  • Loading branch information
rudokemper committed Sep 23, 2024
1 parent cc3640c commit fa34cda
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 76 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
43 changes: 0 additions & 43 deletions src/composables/useAuth.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/imports.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineNuxtModule<ModuleOptions>({
const { resolve } = createResolver(import.meta.url);

// Add composables directory
addImportsDir(resolve("./composables"));
addImportsDir(resolve("./runtime/composables"));

// Add components directory
addComponentsDir({
Expand Down
36 changes: 36 additions & 0 deletions src/runtime/composables/useAuth.ts
Original file line number Diff line number Diff line change
@@ -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;
};
1 change: 1 addition & 0 deletions src/runtime/middleware/oauth.global.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
defineNuxtRouteMiddleware,
useRuntimeConfig,
useUserSession,
navigateTo,
} from "#imports";

Expand Down
53 changes: 24 additions & 29 deletions src/runtime/server/middleware/apiAuth.ts
Original file line number Diff line number Diff line change
@@ -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",
});
}
});

0 comments on commit fa34cda

Please sign in to comment.