All files / src/lib/routes/auth reset-password-controller.ts

76.92% Statements 20/26
100% Branches 1/1
71.43% Functions 5/7
76.92% Lines 20/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82  59x         59x                                           141x 141x     141x 141x 141x 141x 141x                                         7x 7x 6x 6x             6x 6x 6x 3x       12x 12x         59x  
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<PARAMS, QUERY, BODY, K>
    extends Request<PARAMS, QUERY, BODY, K> {
    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<void> {
        const { email } = req.body;
 
        await this.userService.createResetPasswordEmail(email);
        res.status(200).end();
    }
 
    async validatePassword(req: Request, res: Response): Promise<void> {
        const { password } = req.body;
 
        this.userService.validatePassword(password);
        res.status(200).end();
    }
 
    async validateToken(
        req: Request<unknown, unknown, unknown, IValidateQuery>,
        res: Response,
    ): Promise<void> {
        const { token } = req.query;
        const user = await this.userService.getUserForToken(token);
        await this.logout(req);
        res.status(200).json(user);
    }
 
    async changePassword(
        req: Request<unknown, unknown, IChangePasswordBody, unknown>,
        res: Response,
    ): Promise<void> {
        await this.logout(req);
        const { token, password } = req.body;
        await this.userService.resetPassword(token, password);
        res.status(200).end();
    }
 
    private async logout(req: SessionRequest<any, any, any, any>) {
        if (req.session) {
            req.session.destroy(() => {});
        }
    }
}
 
export default ResetPasswordController;