-
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.
feat: message handler for password recovery/reset
Relates #147
- Loading branch information
1 parent
3227fb8
commit 0ae7738
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
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,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 |