import { Request, Response } from 'express'; import Controller from '../controller'; import UserService from '../../services/user-service'; import { Logger } from '../../logger'; import { IUnleashConfig } from '../../types/option'; import { IUnleashServices } from '../../types/services'; import { NONE } from '../../types/permissions'; interface IValidateQuery { token: string; } interface IChangePasswordBody { token: string; password: string; } interface SessionRequest extends Request { user?; } class ResetPasswordController extends Controller { private userService: UserService; private logger: Logger; constructor(config: IUnleashConfig, { userService }: IUnleashServices) { super(config); this.logger = config.getLogger( 'lib/routes/auth/reset-password-controller.ts', ); this.userService = userService; this.get('/validate', this.validateToken); this.post('/password', this.changePassword, NONE); this.post('/validate-password', this.validatePassword, NONE); this.post('/password-email', this.sendResetPasswordEmail, NONE); } async sendResetPasswordEmail(req: Request, res: Response): Promise { const { email } = req.body; await this.userService.createResetPasswordEmail(email); res.status(200).end(); } async validatePassword(req: Request, res: Response): Promise { const { password } = req.body; this.userService.validatePassword(password); res.status(200).end(); } async validateToken( req: Request, res: Response, ): Promise { const { token } = req.query; const user = await this.userService.getUserForToken(token); await this.logout(req); res.status(200).json(user); } async changePassword( req: Request, res: Response, ): Promise { await this.logout(req); const { token, password } = req.body; await this.userService.resetPassword(token, password); res.status(200).end(); } private async logout(req: SessionRequest) { if (req.session) { req.session.destroy(() => {}); } } } export default ResetPasswordController;