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

Created user login page and added password hashing in the db and enabled login [BG, IK]

parent b020fbd9
Pipeline #202347 passed with stage
in 1 minute and 5 seconds
from flask import Blueprint, request, jsonify, Response
from backend.database_config.database import DB
from backend.models.user_model import User
import json
import bcrypt
user = Blueprint('user', __name__)
......@@ -46,7 +46,10 @@ def add_user():
request.json['email'], request.json['bio'], request.json['joined'],
request.json['location'],request.json['availability'],
request.json['partnership_opportunities'], request.json['interests'])
entry = User(username = username, firstname = firstname, surname = surname, password = password,
salt = bcrypt.gensalt()
hash_pswd = bcrypt.hashpw(password.encode('utf-8'), salt).decode('utf-8')
entry = User(username = username, firstname = firstname, surname = surname, password = hash_pswd,
title = title, email = email, bio = bio, joined = joined, location = location,
availability = availability, partnership_opportunities = partnership_opportunities,
interests = interests)
......@@ -67,7 +70,12 @@ def update_user(username):
request.json['partnership_opportunities'], request.json['interests'])
entry.firstname = firstname
entry.surname = surname
entry.password = password
salt = bcrypt.gensalt()
hash_pswd = bcrypt.hashpw(password.encode('utf-8'), salt)
entry.password = hash_pswd
entry.title = title
entry.email = email
entry.bio = bio
......@@ -87,3 +95,14 @@ def delete_user(username):
return ''
@user.route('/users/login', methods = ['POST'])
def user_login():
username, password = (request.json['username'], request.json['password'])
user = User.query.get(username)
if user == None:
return jsonify(status = "False")
if bcrypt.checkpw(password.encode('utf-8'), user.password.encode('utf-8')):
return jsonify(status = "True")
else:
return jsonify(status = "False")
\ No newline at end of file
This diff is collapsed.
......@@ -21,6 +21,7 @@
"babel-loader": "8.1.0",
"babel-plugin-named-asset-import": "^0.3.7",
"babel-preset-react-app": "^10.0.0",
"bcrypt": "^5.0.1",
"bfj": "^7.0.2",
"camelcase": "^6.1.0",
"case-sensitive-paths-webpack-plugin": "2.3.0",
......
......@@ -43,7 +43,7 @@ const AddUser = () => {
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newUser)
}).then(() => {
history.push('/');
history.push('/home');
})
}
......
......@@ -13,17 +13,21 @@ import Sidebar from './Sidebar'
import EditUser from './EditUser'
import ProjectTasks from './ProjectTasks'
import AddProjectTask from './AddProjectTask'
import Login from './Login'
function App() {
const title = "Dashboard"
return (
<Router>
<div className="App">
<div className="App">
<Sidebar />
<Navbar />
<div className="content">
<Switch>
<Navbar />
<div className="content">
<Switch>
<Route exact path="/">
<Login />
</Route>
<Route exact path="/home">
<Home />
</Route>
<Route exact path="/create-project">
......
......@@ -21,7 +21,7 @@ const CreateProject = () => {
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newProject)
}).then(() => {
history.push('/');
history.push('/home');
})
}
......
......@@ -26,7 +26,7 @@ const EditProject = () => {
headers: { "Content-Type": "application/json" },
body: JSON.stringify(e)
}).then(() => {
history.push('/');
history.push('/home');
})
}
......
......@@ -32,7 +32,7 @@ const EditUser = () => {
headers: { "Content-Type": "application/json" },
body: JSON.stringify(e)
}).then(() => {
history.push('/');
history.push('/home');
})
}
......
import { useState } from 'react';
import { useHistory } from "react-router-dom"
import useFetch from './useFetch'
const Login = ({ setToken }) => {
const [username, setUserName] = useState();
const [password, setPassword] = useState();
const history = useHistory()
const handleSubmit = async e => {
e.preventDefault();
const credentials = {username, password}
fetch('/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(credentials)
})
.then(res => {
if (!res.ok) { // error coming back from server
throw Error('could not fetch the data for that resource');
}
return res.json();
})
.then(
data => {
if(data.status == "True") {
history.push('/home');
} else {
document.getElementsByClassName("incorrect-password")[0].style.display = "block";
}
})
}
return (
<div className="login">
<h2> Sign In </h2>
<form>
<input type="text" id="login" class="fadeIn second" name="login" placeholder="login"
onChange={e => setUserName(e.target.value)}/>
<input type="text" id="password" class="fadeIn third" name="login" placeholder="password"
onChange={e => setPassword(e.target.value)}/>
<div className="incorrect-password" style={{display: 'none'}}>
<h2 >Incorrect password!</h2>
</div>
<button onClick={handleSubmit}>Log In</button>
</form>
<div id="formFooter">
<a class="underlineHover" href="#">Forgot Password?</a>
</div>
</div>
);
}
export default Login;
import { Link } from 'react-router-dom'
const Navbar = () => {
if (window.location.pathname.match('/')){
return null;
}
return (
<nav className="navbar" >
<a href="/" style={{
<a href="/home" style={{
color: '#ff4e83',
fontSize: '20px',
borderRadius: '8px'
......
......@@ -5,7 +5,7 @@ const PageNotFound = () => {
<div className="page-not-found">
<h2>Something went wrong...</h2>
<p>That page cannot be found</p>
<Link to='/'>Back to the main page</Link>
<Link to='/home'>Back to the main page</Link>
</div>
);
}
......
......@@ -11,7 +11,7 @@ const ProjectDetails = () => {
const handleRemove = e => {
e.preventDefault()
fetch("/projects/" + id, { method: 'DELETE' })
.then(() => { history.push('/'); })
.then(() => { history.push('/home'); })
};
return (
......
......@@ -5,6 +5,9 @@ import { Link } from 'react-router-dom'
const slackLink = "https://slack.com/intl/en-gb/"
export default props => {
if (window.location.pathname.match('/')){
return null;
}
return (
<Menu>
<div className="menu-item" >
......@@ -21,7 +24,8 @@ export default props => {
</div>
<div className="menu-item" >
<a href={slackLink} target="_blank">Go To Slack</a>
</div>
</div>
</Menu>
);
};
\ No newline at end of file
......@@ -11,7 +11,7 @@ const User = () => {
const handleRemove = e => {
e.preventDefault()
fetch("/users/" + username, {method: 'DELETE'})
.then(()=> {history.push('/');})
.then(()=> {history.push('/home');})
};
return (
......
......@@ -6,4 +6,5 @@ Flask-Script==2.0.6
Flask-Migrate==3.0.1
psycopg2-binary==2.8.6
pytest==6.2.4
pytest-order==0.11.0
\ No newline at end of file
pytest-order==0.11.0
bcrypt==3.2.0
\ No newline at end of file
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