Commit 1b5fd19a authored by Bianca-Mihaela Ganescu's avatar Bianca-Mihaela Ganescu
Browse files

Started implementing change password feature in edit user page [BG, IK]

parent 06058953
Pipeline #202760 passed with stage
in 1 minute and 6 seconds
......@@ -105,4 +105,24 @@ def user_login():
if bcrypt.checkpw(password.encode('utf-8'), user.password.encode('utf-8')):
return jsonify(token = username)
else:
return jsonify(token = "")
\ No newline at end of file
return jsonify(token = "")
@user.route('/users/update-password', methods = ['POST'])
def change_user_password():
username, oldPassword, newPassword = (request.json['username'], request.json['oldPassword'],
request.json['newPassword'])
user = User.query.get(username)
if user == None:
return jsonify(status = "False")
if bcrypt.checkpw(oldPassword.encode('utf-8'), user.password.encode('utf-8')):
salt = bcrypt.gensalt()
hash_pswd = bcrypt.hashpw(newPassword.encode('utf-8'), salt)
user.password = hash_pswd
return jsonify(status = "True")
else:
return jsonify(status = "False")
......@@ -17,6 +17,7 @@ import ProjectTasks from './ProjectTasks'
import AddProjectTask from './AddProjectTask'
import Login from './Login'
import useToken from './useToken';
import ChangePassword from './ChangePassword';
function App() {
......@@ -53,6 +54,9 @@ function App() {
</Route>
<Route exact path="/edit-user/:username">
<EditUser />
</Route>
<Route exact path="/users/change-password">
<ChangePassword />
</Route>
<Route exact path="/projects/:id/add-task">
<AddProjectTask />
......
import useToken from './useToken';
import { useHistory } from 'react-router-dom'
import useState from 'react'
const ChangePassword = () =>{
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const {setToken, token} = useToken()
const history = useHistory()
async function passwordUpdate() {
return fetch('/users/update-password', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(
token,
oldPassword,
newPassword)
})
.then(data => data.json())
}
const handleSubmit = async e => {
e.preventDefault();
const updated = await passwordUpdate();
if(updated.status != "True") {
document.getElementsByClassName("incorrect-password")[0].style.display = "block";
}
else{
history.push('/');
}
}
return (
<div className="change-password">
<h2>Change Password</h2>
<label >Current Password</label>
<input
onChange={(e) => setOldPassword(e.target.value)}
placeholder="Current Password"
/>
<label >New Password</label>
<input
onChange={(e) => setNewPassword(e.target.value)}
placeholder="New Password"
/>
<button onClick={handleSubmit}>Change password</button>
<h2 className="incorrect-password" style={{display: "none"}}>Incorrect password</h2>
</div>
)
}
export default ChangePassword;
\ No newline at end of file
import { useHistory, useParams } from "react-router-dom"
import { Link, useHistory, useParams } from "react-router-dom"
import { useForm } from "react-hook-form"
import useFetch from './useFetch'
......@@ -40,6 +40,7 @@ const EditUser = () => {
<div className="edit-user">
<h2>Edit user</h2>
{ isPending && <h2>Loading...</h2> }
<Link to={`/users/change-password`}>Change Password</Link>
{ firstname && <form onSubmit={handleSubmit(onSubmit)}>
<label htmlFor="firstname">Firstname</label>
<input {...register("firstname")}
......
import React from 'react';
import { slide as Menu } from 'react-burger-menu';
import { Link, useHistory } from 'react-router-dom'
import useToken from './useToken';
const Sidebar = () => {
......@@ -8,6 +9,8 @@ const slackLink = "https://slack.com/intl/en-gb/"
const history = useHistory();
const {setToken, token} = useToken();
const handleSignOut = e => {
e.preventDefault()
......@@ -23,7 +26,7 @@ return (
<Link to={`/`}>Home</Link>
</div>
<div className="menu-item" >
<Link to={`/users/john34`}>Profile</Link>
<Link to={`/users/` + token}>Profile</Link>
</div>
<div className="menu-item" >
<Link to={`/users`}>All Users</Link>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment