-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
133 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 { StatusCodes } from 'http-status-codes'; | ||
import User from '../../../../models/user.js'; | ||
|
||
export default async function (fastify, _opts) { | ||
fastify.post( | ||
'/password-reset', | ||
{ | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['passwordResetToken', 'password'], | ||
properties: { | ||
passwordResetToken: { type: 'string' }, | ||
password: { type: 'string' }, | ||
}, | ||
}, | ||
response: { | ||
[StatusCodes.OK]: { | ||
type: 'null', | ||
}, | ||
[StatusCodes.UNAUTHORIZED]: { | ||
type: 'object', | ||
properties: { | ||
message: { type: 'string' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const { passwordResetToken, password } = request.body; | ||
|
||
const data = await fastify.prisma.user.findUnique({ | ||
where: { passwordResetToken }, | ||
}); | ||
|
||
if (!data) { | ||
return reply.unauthorized( | ||
'Password Reset Link is expired or not valid', | ||
); | ||
} | ||
|
||
const user = new User(data); | ||
|
||
await user.setPassword(password); | ||
|
||
await fastify.prisma.user.update({ | ||
where: { passwordResetToken }, | ||
data: { | ||
passwordResetToken: null, | ||
passwordResetExpires: null, | ||
hashedPassword: user.hashedPassword, | ||
}, | ||
}); | ||
|
||
reply.code(StatusCodes.OK); | ||
}, | ||
); | ||
} |
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