1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 07:25:19 +02:00

add reset handler

This commit is contained in:
Enes Kaya 2024-01-20 17:01:00 +01:00
parent 6393bbdc85
commit 3bbef92316
No known key found for this signature in database
GPG key ID: 6E2D8C8DAC586FF3
4 changed files with 55 additions and 39 deletions

View 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())
}

View file

@ -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('')

View file

@ -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

View file

@ -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({