Created
March 26, 2024 21:01
-
-
Save antonkalik/6eea5e6d275e574e8f189f1b2b82fe6e to your computer and use it in GitHub Desktop.
Forgot Password Controller
This file contains hidden or 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
import { Request, Response } from 'express'; | |
import { UserModel } from 'src/models/UserModel'; | |
import type { User } from 'src/@types'; | |
import { TokenService } from 'src/services/TokenService'; | |
import { EmailService } from 'src/services/EmailService'; | |
export const forgotPasswordController = async (req: Request, res: Response) => { | |
try { | |
const { | |
email, | |
}: { | |
email: string; | |
} = req.body; | |
const user = await UserModel.findByEmail(email); | |
if (user) { | |
const token = await TokenService.sign( | |
{ | |
id: user.id, | |
}, | |
{ | |
expiresIn: '1 day', | |
} | |
); | |
await user.context.update({ forgot_password_token: token }); | |
await EmailService.sendPasswordResetEmail(email, token); | |
} | |
return res.sendStatus(200); | |
} catch (error) { | |
return res.sendStatus(500); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment