added ability to change password
This commit is contained in:
		@@ -15,7 +15,7 @@ class UserController {
 | 
			
		||||
 | 
			
		||||
        if (password != passwordConfirm) return res.status(400).send("Passwords do not match");
 | 
			
		||||
 | 
			
		||||
        let hashedPassword = await bcrypt.hash(password, 8);
 | 
			
		||||
        const hashedPassword = await bcrypt.hash(password, 8);
 | 
			
		||||
 | 
			
		||||
        await UserService.register(username, hashedPassword);
 | 
			
		||||
 | 
			
		||||
@@ -39,6 +39,24 @@ class UserController {
 | 
			
		||||
        return res.redirect("/index");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async changePassword(req, res) {
 | 
			
		||||
        const token = req.session.jwt;
 | 
			
		||||
        const username = jwt.decode(token).username;
 | 
			
		||||
        const { oldPassword, newPassword } = req.body;
 | 
			
		||||
 | 
			
		||||
        const storedPassword = await UserService.getPassword(username);
 | 
			
		||||
 | 
			
		||||
        if (!bcrypt.compareSync(oldPassword, storedPassword)) {
 | 
			
		||||
            return res.status(403).send("Password is not correct");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const newHashedPassword = await bcrypt.hash(newPassword, 8);
 | 
			
		||||
 | 
			
		||||
        await UserService.changePassword(username, newHashedPassword);
 | 
			
		||||
 | 
			
		||||
        return res.status(200).send("Successful");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async logout(req, res) {
 | 
			
		||||
        req.session.destroy();
 | 
			
		||||
        return res.redirect("/login");
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user