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';
|
2022-06-22 13:31:41 +02:00
|
|
|
import { IUnleashServices } from '../../types';
|
2021-12-03 12:46:50 +01:00
|
|
|
import { NONE } from '../../types/permissions';
|
2022-06-22 13:31:41 +02:00
|
|
|
import { createRequestSchema, createResponseSchema } from '../../openapi';
|
|
|
|
import { emptyResponse } from '../../openapi/spec/empty-response';
|
|
|
|
import { OpenApiService } from '../../services/openapi-service';
|
|
|
|
import {
|
|
|
|
tokenUserSchema,
|
|
|
|
TokenUserSchema,
|
|
|
|
} from '../../openapi/spec/token-user-schema';
|
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
|
|
|
|
2022-06-22 13:31:41 +02:00
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
2021-04-27 09:16:44 +02:00
|
|
|
private logger: Logger;
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2022-06-22 13:31:41 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{
|
|
|
|
userService,
|
|
|
|
openApiService,
|
|
|
|
}: Pick<IUnleashServices, 'userService' | 'openApiService'>,
|
|
|
|
) {
|
2021-04-16 15:29:23 +02:00
|
|
|
super(config);
|
|
|
|
this.logger = config.getLogger(
|
|
|
|
'lib/routes/auth/reset-password-controller.ts',
|
|
|
|
);
|
2022-06-22 13:31:41 +02:00
|
|
|
this.openApiService = openApiService;
|
2021-04-16 15:29:23 +02:00
|
|
|
this.userService = userService;
|
2022-06-22 13:31:41 +02:00
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '/validate',
|
|
|
|
handler: this.validateToken,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['other'],
|
|
|
|
operationId: 'validateToken',
|
|
|
|
responses: { 200: createResponseSchema('tokenUserSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/password',
|
|
|
|
handler: this.changePassword,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['other'],
|
|
|
|
operationId: 'changePassword',
|
|
|
|
requestBody: createRequestSchema('changePasswordSchema'),
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/validate-password',
|
|
|
|
handler: this.validatePassword,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['other'],
|
|
|
|
operationId: 'validatePassword',
|
|
|
|
requestBody: createRequestSchema('validatePasswordSchema'),
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/password-email',
|
|
|
|
handler: this.sendResetPasswordEmail,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['other'],
|
|
|
|
operationId: 'sendResetPasswordEmail',
|
|
|
|
requestBody: createRequestSchema('resetPasswordSchema'),
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>,
|
2022-06-22 13:31:41 +02:00
|
|
|
res: Response<TokenUserSchema>,
|
2021-04-16 15:29:23 +02:00
|
|
|
): 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);
|
2022-06-22 13:31:41 +02:00
|
|
|
this.openApiService.respondWithValidation<TokenUserSchema>(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
tokenUserSchema.$id,
|
|
|
|
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;
|