Skip to content

Commit

Permalink
Merge pull request #14 from icefoganalytics/issue-10/fix-tsconfig-jso…
Browse files Browse the repository at this point in the history
…n-error-around-express-unless

Fix tsconfig.json Error Around `express-unless`
  • Loading branch information
klondikemarlen authored Jan 15, 2024
2 parents 43cac9f + bea23bf commit f0f7eb0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 49 deletions.
21 changes: 0 additions & 21 deletions api/package-lock.json

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

1 change: 0 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@types/cls-hooked": "^4.3.8",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/express-jwt": "^6.0.4",
"@types/lodash": "^4.14.202",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
Expand Down
20 changes: 20 additions & 0 deletions api/src/middlewares/jwt-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expressjwt as jwt } from "express-jwt"
import jwksRsa, { type GetVerificationKey } from "jwks-rsa"

import { AUTH0_DOMAIN, AUTH0_AUDIENCE } from "@/config"

console.log("AUTH0_DOMAIN", `${AUTH0_DOMAIN}/.well-known/jwks.json`)

export default jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `${AUTH0_DOMAIN}/.well-known/jwks.json`,
}) as GetVerificationKey,

// Validate the audience and the issuer.
audience: AUTH0_AUDIENCE,
issuer: [`${AUTH0_DOMAIN}/`],
algorithms: ["RS256"],
})
4 changes: 4 additions & 0 deletions api/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { template } from "lodash"

import { APPLICATION_NAME, GIT_COMMIT_HASH, RELEASE_TAG } from "@/config"

import jwtMiddleware from "@/middlewares/jwt-middleware"

export const router = Router()

// some routes
Expand All @@ -16,6 +18,8 @@ router.route("/_status").get((req: Request, res: Response) => {
})
})

router.use("/api", jwtMiddleware)

// if no other api routes match, send the 404 page
router.use("/api", (req: Request, res: Response) => {
const templatePath = path.resolve(__dirname, "web/404.html")
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ x-default-environment: &default-environment
FRONTEND_URL: "http://localhost:8080"
VITE_APPLICATION_NAME: "Internal Data Portal"
VITE_API_BASE_URL: "http://localhost:3000"
VITE_AUTH0_CLIENT_ID": "mS8zklFSgatWX3v1OCQgVpEq5MixCm4k"
VITE_AUTH0_AUDIENCE": "testing"
VITE_AUTH0_DOMAIN": "https://dev-0tc6bn14.eu.auth0.com"
VITE_AUTH0_CLIENT_ID: "mS8zklFSgatWX3v1OCQgVpEq5MixCm4k"
VITE_AUTH0_AUDIENCE: "testing"
VITE_AUTH0_DOMAIN: "https://dev-0tc6bn14.eu.auth0.com"
RELEASE_TAG: "${RELEASE_TAG:-development}"
GIT_COMMIT_HASH: "${GIT_COMMIT_HASH:-not-set}"

Expand Down
36 changes: 18 additions & 18 deletions web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
</template>

<script lang="ts" setup>
import { useAuth0 } from "@auth0/auth0-vue"
import { watch } from "vue"
import { useRouter } from "vue-router"
// import { useAuth0 } from "@auth0/auth0-vue"
// import { watch } from "vue"
// import { useRouter } from "vue-router"
const router = useRouter()
const { isLoading, isAuthenticated } = useAuth0()
// const router = useRouter()
// const { isLoading, isAuthenticated } = useAuth0()
// TODO: this is hacked-together, it should reworked before final usage
watch(
() => [isLoading.value, isAuthenticated.value],
([newIsLoading, newIsAuthenticated], _) => {
if (newIsLoading === false && newIsAuthenticated === true) {
router.push("/dashboard")
} else if (!newIsLoading && !isAuthenticated.value) {
router.push("/sign-in")
}
},
{
immediate: true,
}
)
// watch(
// () => [isLoading.value, isAuthenticated.value],
// ([newIsLoading, newIsAuthenticated], _) => {
// if (newIsLoading === false && newIsAuthenticated === true) {
// router.push("/dashboard")
// } else if (!newIsLoading && !isAuthenticated.value) {
// router.push("/sign-in")
// }
// },
// {
// immediate: true,
// }
// )
</script>
14 changes: 8 additions & 6 deletions web/src/api/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import qs from "qs"
import axios from "axios"

import { API_BASE_URL } from "@/config"
import { useAuth0 } from "@auth0/auth0-vue"

const { getAccessTokenSilently, loginWithRedirect } = useAuth0()
import auth0 from "@/plugins/auth0-plugin"

export const httpClient = axios.create({
baseURL: API_BASE_URL,
Expand All @@ -19,8 +17,12 @@ export const httpClient = axios.create({
})

httpClient.interceptors.request.use(async (config) => {
const accessToken = await getAccessTokenSilently()
config.headers["Authorization"] = `Bearer ${accessToken}`
// Only add the Authorization header to requests that start with "/api"
if (config.url?.startsWith("/api")) {
const accessToken = await auth0.getAccessTokenSilently()
config.headers["Authorization"] = `Bearer ${accessToken}`
}

return config
})

Expand All @@ -30,7 +32,7 @@ httpClient.interceptors.response.use(null, async (error) => {
// Bounce the user if they hit a login required error when trying to access a protected route
// It would probably be better to move this code to a route guard or something?
if (error?.error === "login_required") {
await loginWithRedirect({
await auth0.loginWithRedirect({
appState: { targetUrl: window.location.pathname },
})
} else if (error?.response?.data?.message) {
Expand Down

0 comments on commit f0f7eb0

Please sign in to comment.