-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix all runtime code for usage with built Nuxt projects
- Loading branch information
1 parent
cc3640c
commit fa34cda
Showing
8 changed files
with
72 additions
and
76 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { | ||
defineNuxtRouteMiddleware, | ||
useRuntimeConfig, | ||
useUserSession, | ||
navigateTo, | ||
} from "#imports"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); | ||
} | ||
}); |