All files / src/lib/routes/auth simple-password-provider.ts

100% Statements 14/14
100% Branches 3/3
100% Functions 2/2
100% Lines 14/14

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          60x 60x                       144x 144x 144x   144x       3x   3x 1x     1x     2x 1x 1x       60x  
import { Response } from 'express';
import { Logger } from '../../logger';
import { IUnleashConfig } from '../../server-impl';
import UserService from '../../services/user-service';
import { IUnleashServices } from '../../types';
import { NONE } from '../../types/permissions';
import Controller from '../controller';
import { IAuthRequest } from '../unleash-types';
 
class PasswordProvider extends Controller {
    private userService: UserService;
 
    private logger: Logger;
 
    constructor(
        config: IUnleashConfig,
        { userService }: Pick<IUnleashServices, 'userService'>,
    ) {
        super(config);
        this.logger = config.getLogger('/auth/password-provider.js');
        this.userService = userService;
 
        this.post('/login', this.login, NONE);
    }
 
    async login(req: IAuthRequest, res: Response): Promise<void> {
        const { username, password } = req.body;
 
        if (!username || !password) {
            res.status(400).json({
                message: 'You must provide username and password',
            });
            return;
        }
 
        const user = await this.userService.loginUser(username, password);
        req.session.user = user;
        res.status(200).json(user);
    }
}
 
export default PasswordProvider;