mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-08 23:15:24 +02:00
add reset handler
This commit is contained in:
parent
6393bbdc85
commit
3bbef92316
4 changed files with 55 additions and 39 deletions
15
apps/client/pages/api/auth/request-password-reset.ts
Normal file
15
apps/client/pages/api/auth/request-password-reset.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import type { NextApiResponse } from 'next'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import env from '../../../env'
|
||||
|
||||
export default async function handler(req: NextRequest, res: NextApiResponse) {
|
||||
const r = await fetch(`${env.NEXT_PUBLIC_API_URL}/v1/request-new-password`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(req.body),
|
||||
})
|
||||
|
||||
return res.status(200).json(await r.json())
|
||||
}
|
|
@ -58,15 +58,15 @@ export default function LoginPage() {
|
|||
const sendResetPasswordEmail = async () => {
|
||||
setSendResetPasswordEmailLoading(true)
|
||||
|
||||
const response = await fetch('/api/auth/reset-password', {
|
||||
const response = await fetch('/api/auth/request-password-reset', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
email: forgotPasswordEmail,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ email: forgotPasswordEmail }),
|
||||
})
|
||||
|
||||
if (response.ok) {
|
||||
if (response.status === 200) {
|
||||
setShowResetPasswordSuccess(true)
|
||||
setSendResetPasswordEmailLoading(false)
|
||||
setForgotPasswordEmail('')
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { Router } from 'express'
|
||||
import { z } from 'zod'
|
||||
import env from '../../env'
|
||||
import endpoint from '../lib/endpoint'
|
||||
|
||||
|
@ -18,4 +19,37 @@ router.get(
|
|||
})
|
||||
)
|
||||
|
||||
router.post(
|
||||
'/request-new-password',
|
||||
endpoint.create({
|
||||
input: z.object({
|
||||
email: z.string().email(),
|
||||
}),
|
||||
resolve: async ({ ctx, input }) => {
|
||||
if (ctx.user) return
|
||||
await ctx.authPasswordResetService.create(input.email)
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
router.post(
|
||||
'/reset-password/:token/:email',
|
||||
endpoint.create({
|
||||
input: z.object({
|
||||
// TODO: bring en par with required password schema
|
||||
// (1 lowercase, 1 uppercase, 1 special char)
|
||||
newPassword: z.string().min(8).max(64),
|
||||
confirmPassword: z.string().min(8).max(64),
|
||||
}),
|
||||
resolve: async ({ ctx, input, req }) => {
|
||||
if (ctx.user) return
|
||||
await ctx.authPasswordResetService.resetPassword({
|
||||
token: req.params.token,
|
||||
newPassword: input.newPassword,
|
||||
email: req.params.email,
|
||||
})
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
export default router
|
||||
|
|
|
@ -388,39 +388,6 @@ router.delete(
|
|||
})
|
||||
)
|
||||
|
||||
router.post(
|
||||
'/request-new-password',
|
||||
endpoint.create({
|
||||
input: z.object({
|
||||
email: z.string().email(),
|
||||
}),
|
||||
resolve: async ({ ctx, input }) => {
|
||||
if (ctx.user) return
|
||||
await ctx.authPasswordResetService.create(input.email)
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
router.post(
|
||||
'/reset-password/:token/:email',
|
||||
endpoint.create({
|
||||
input: z.object({
|
||||
// TODO: bring en par with required password schema
|
||||
// (1 lowercase, 1 uppercase, 1 special char)
|
||||
newPassword: z.string().min(8).max(64),
|
||||
confirmPassword: z.string().min(8).max(64),
|
||||
}),
|
||||
resolve: async ({ ctx, input, req }) => {
|
||||
if (ctx.user) return
|
||||
await ctx.authPasswordResetService.resetPassword({
|
||||
token: req.params.token,
|
||||
newPassword: input.newPassword,
|
||||
email: req.params.email,
|
||||
})
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
router.delete(
|
||||
'/:id',
|
||||
endpoint.create({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue