Skip to content

Commit

Permalink
feat: message handler for password recovery/reset
Browse files Browse the repository at this point in the history
Relates #147
  • Loading branch information
nichgalzin committed Dec 18, 2024
1 parent 3227fb8 commit 0ae7738
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/background/messages/passwordRecovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { PlasmoMessaging } from "@plasmohq/messaging"

interface SuccessResponse {
ok: boolean
}

interface ErrorDetail {
path: string[]
message: string
name: string
}

interface ErrorDetails {
errors: ErrorDetail[]
}

interface ErrorObject {
status: number
name: string
message: string
details: ErrorDetails
}

interface ErrorResponse {
data: null
error: ErrorObject
}

function isErrorResponse(
response: SuccessResponse | ErrorResponse
): response is ErrorResponse {
return "error" in response
}

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const response = await fetch(
`${process.env.PLASMO_PUBLIC_API_URL}/api/auth/forgot-password`,
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: req.body
}
)

const returnedData: SuccessResponse | ErrorResponse =
await response.json()

if (isErrorResponse(returnedData)) {
console.error(returnedData.error.message)
res.send({ error: returnedData.error.message })
return
}

res.send(returnedData)
}

export default handler

0 comments on commit 0ae7738

Please sign in to comment.