2021-04-16 15:29:23 +02:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import Controller from '../controller';
|
|
|
|
import UserService from '../../services/user-service';
|
|
|
|
import { Logger } from '../../logger';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
2021-04-27 09:16:44 +02:00
|
|
|
import { IUnleashServices } from '../../types/services';
|
2021-04-16 15:29:23 +02:00
|
|
|
|
|
|
|
interface IValidateQuery {
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IChangePasswordBody {
|
|
|
|
token: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
2021-04-27 09:16:44 +02:00
|
|
|
interface SessionRequest<PARAMS, QUERY, BODY, K>
|
|
|
|
extends Request<PARAMS, QUERY, BODY, K> {
|
|
|
|
user?;
|
|
|
|
}
|
|
|
|
|
2021-04-16 15:29:23 +02:00
|
|
|
class ResetPasswordController extends Controller {
|
2021-04-27 09:16:44 +02:00
|
|
|
private userService: UserService;
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 09:16:44 +02:00
|
|
|
private logger: Logger;
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 09:16:44 +02:00
|
|
|
constructor(config: IUnleashConfig, { userService }: IUnleashServices) {
|
2021-04-16 15:29:23 +02:00
|
|
|
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);
|
|
|
|
this.post('/validate-password', this.validatePassword);
|
|
|
|
this.post('/password-email', this.sendResetPasswordEmail);
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendResetPasswordEmail(req: Request, res: Response): Promise<void> {
|
|
|
|
const { email } = req.body;
|
|
|
|
|
2021-08-13 10:36:19 +02:00
|
|
|
await this.userService.createResetPasswordEmail(email);
|
|
|
|
res.status(200).end();
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async validatePassword(req: Request, res: Response): Promise<void> {
|
|
|
|
const { password } = req.body;
|
|
|
|
|
2021-08-13 10:36:19 +02:00
|
|
|
this.userService.validatePassword(password);
|
|
|
|
res.status(200).end();
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async validateToken(
|
|
|
|
req: Request<unknown, unknown, unknown, IValidateQuery>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
|
|
|
const { token } = req.query;
|
2021-08-13 10:36:19 +02:00
|
|
|
const user = await this.userService.getUserForToken(token);
|
|
|
|
await this.logout(req);
|
|
|
|
res.status(200).json(user);
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async changePassword(
|
|
|
|
req: Request<unknown, unknown, IChangePasswordBody, unknown>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
2021-04-27 09:16:44 +02:00
|
|
|
await this.logout(req);
|
2021-04-16 15:29:23 +02:00
|
|
|
const { token, password } = req.body;
|
2021-08-13 10:36:19 +02:00
|
|
|
await this.userService.resetPassword(token, password);
|
|
|
|
res.status(200).end();
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|
2021-04-27 09:16:44 +02:00
|
|
|
|
|
|
|
private async logout(req: SessionRequest<any, any, any, any>) {
|
|
|
|
if (req.session) {
|
2021-08-12 15:04:37 +02:00
|
|
|
req.session.destroy(() => {});
|
2021-04-27 09:16:44 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ResetPasswordController;
|
|
|
|
module.exports = ResetPasswordController;
|