All files / app/routes/api/user/settings/change-password change-password-controller.ts

100% Statements 19/19
100% Branches 6/6
100% Functions 1/1
100% Lines 19/19

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  1x 1x 1x   1x 1x   1x 7x     7x       2x 2x     5x 5x 5x   5x 4x 2x   1x 1x       1x  
import {RequestHandler} from 'express';
import {BadRequestError} from '@errors';
import debug from 'debug';
import {UserService} from '@models';
 
const log = debug('group-car:user:settings:change-password');
const error = debug('group-car:user:settings:change-password:error');
 
const ChangePasswordController: RequestHandler = async (req, res, next) => {
  log('IP %d requests password change', req.ip);
 
  // Check if request body has the two required fields
  if (
    typeof req.body.oldPassword !== 'string' ||
    typeof req.body.newPassword !== 'string'
  ) {
    error('At least one parameter is missing');
    throw new BadRequestError();
  }
 
  const user = req.user;
  const oldPassword = req.body.oldPassword;
  const newPassword = req.body.newPassword;
 
  if (typeof user === 'object') {
    await UserService.changePassword(user, oldPassword, newPassword);
    res.status(204).send();
  } else {
    error('User not set on request');
    throw new BadRequestError();
  }
};
 
export default ChangePasswordController;