Skip to content

Commit

Permalink
feat: remove error message
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Sep 27, 2024
1 parent 5063b34 commit 933ac4f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
15 changes: 4 additions & 11 deletions packages/api/src/functions/signin/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@
*/
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import schema from "./schema"
import { initializeApp } from "firebase-admin/app"
import { getAuth } from "firebase-admin/auth"
import { getAdminNano, getOrCreateUserFromEmail } from "@libs/couch/dbHelpers"
import { generateToken } from "@libs/auth"
import { ObokuErrorCode } from "@oboku/shared"
import { createHttpError } from "@libs/httpErrors"
import { getParametersValue } from "@libs/ssm"
import { withMiddy } from "@libs/middy/withMiddy"

const firebaseConfig = JSON.parse(
Buffer.from(process.env.FIREBASE_CONFIG ?? "", "base64").toString() ?? "{}"
)

/**
* This is an admin without privileges
*/
const app = initializeApp(firebaseConfig)
import { getFirebaseApp } from "@libs/firebase/app"

const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
event
Expand All @@ -33,8 +24,10 @@ const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
})

const { token } = event.body
const firebaseAopp = getFirebaseApp()

const { email, email_verified } = await getAuth(app).verifyIdToken(token)
const { email, email_verified } =
await getAuth(firebaseAopp).verifyIdToken(token)

if (!email) {
throw createHttpError(400, {
Expand Down
24 changes: 24 additions & 0 deletions packages/api/src/libs/firebase/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { initializeApp } from "firebase-admin/app"

let app: ReturnType<typeof initializeApp> | undefined

/**
* We keep the app as a singleton to optimize its instanciation
* across lambda reuse.
*
* Do not call it outside of a lambda to mitigate uncaught error.
*/
export const getFirebaseApp = () => {
if (app) return app

const firebaseConfig = JSON.parse(
Buffer.from(process.env.FIREBASE_CONFIG ?? "", "base64").toString() ?? "{}"
)

/**
* This is an admin without privileges
*/
app = initializeApp(firebaseConfig)

return app
}
8 changes: 1 addition & 7 deletions packages/api/src/libs/middy/unexpectedErrorToHttpError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ export const unexpectedErrorToHttpError = (): MiddlewareObj => ({
onError: async (request) => {
if (request.error) {
console.error("error received", request.error)
}

if (request.error) {
request.error = createHttpError(500, {
code: ObokuErrorCode.UNKNOWN,
message: request.error.message
message: process.env.NODE_ENV === "development" ? request.error.message : ``
})

// eslint-disable-next-line no-extra-semi
;(request.error as any).expose =
(request.error as any)?.expose ?? process.env.NODE_ENV === "development"
}
}
})

0 comments on commit 933ac4f

Please sign in to comment.