diff --git a/backend/models/userModel.js b/backend/models/userModel.js index 43af5a62f309dbaddcf73ab7cd1174e6d2b58757..27ea0491641f7670e19437feb750ddaba20a7632 100644 --- a/backend/models/userModel.js +++ b/backend/models/userModel.js @@ -2,9 +2,10 @@ const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const UserSchema = new mongoose.Schema({ - username:String, + username: String, password: String, - physio: Boolean + physio: Boolean, + code: String }) //Here we'll do the encryption diff --git a/backend/routes/authRoutes.js b/backend/routes/authRoutes.js index 152945a2b5d7102ad6beb2223d989312d6e6744f..01456eb9999d648d445e06f493321c029f95f2c5 100644 --- a/backend/routes/authRoutes.js +++ b/backend/routes/authRoutes.js @@ -23,6 +23,7 @@ router.post('/signup', (req, res)=>{ const username = req.body.username; const password = req.body.password; const physio = req.body.physio; + const code = req.body.code; userFromDb.findOne({'username':username}, function(err, result){ if(err){ console.log("Error with the database");}; @@ -31,9 +32,25 @@ router.post('/signup', (req, res)=>{ return res.status(422).send('Invalid username') } else{ - const user = new userFromDb({username:username, password:password, physio:physio}); - user.save(); - res.status(200).send('User registered'); + if (physio){ + userFromDb.findOne({'code':code}, function(err, result){ + if(err){ console.log("Error with the database");}; + + if (result!=null){ + return res.status(423).send('Invalid code') + } + else{ + const user = new userFromDb({username:username, password:password, physio:physio, code:code}); + user.save(); + res.status(200).send('User registered'); + } + }) + } + else{ + const user = new userFromDb({username:username, password:password, physio:physio, code:code}); + user.save(); + res.status(200).send('User registered'); + } } }) }) @@ -55,8 +72,12 @@ router.post('/signin', (req, res)=>{ if (err) throw err; //console.log(password, isMatch); if (isMatch){ - res.status(200).send('ok'); - console.log(result.physio); + if (result.physio){ + res.status(201).send('Physio'); + } + else { + res.status(200).send(JSON.stringify({username: "Luca"})); + } }else{ return res.status(422).send('Invalid username or password'); } diff --git a/mobile/Screens/loginScreen.js b/mobile/Screens/loginScreen.js index 9d465efbb627c1d9021281d115abbdd4b3a04baa..c02cfd08a73df8801d066536b1e0cc83f257d352 100644 --- a/mobile/Screens/loginScreen.js +++ b/mobile/Screens/loginScreen.js @@ -12,7 +12,6 @@ class LoginScreen extends Component { submitLogin() { - const bool = true; //here we're going to post the username and password inserted in the //login page, in particulare this is a post request to the /signin route //in the server that will response with status:200 if the credentials are in the @@ -27,18 +26,21 @@ class LoginScreen extends Component { password: this.state.password }), }) //here we handle the status response of the server + /*.then(res => res.json()) + .then((data => { + alert(data.username) + }))*/ .then(res => { - if (res.status !== 200) { - alert('Invalid username or password'); + if (res.status == 200) { + alert('Succesful login') + this.props.navigation.navigate('Chat') } - else { + else if (res.status == 201){ alert('Succesful login') - if (bool){ - this.props.navigation.navigate('test') - } - else{ - this.props.navigation.navigate('Chat') - } + this.props.navigation.navigate('test') + } + else { + alert('Unsuccesful login') } }) //here we set again username and password as blank diff --git a/mobile/Screens/registrationScreen.js b/mobile/Screens/registrationScreen.js index f5a805d308608edbf5b5babed82e0410c4f3c4c8..6025d6aab111649e6dc11dbccfd7f5a5ed5ed7f3 100644 --- a/mobile/Screens/registrationScreen.js +++ b/mobile/Screens/registrationScreen.js @@ -7,7 +7,8 @@ class RegistrationScreen extends Component { this.state = { username: "", password: "", - physio: false + physio: false, + code: "" }; } @@ -23,12 +24,18 @@ class RegistrationScreen extends Component { body: JSON.stringify({ username : this.state.username, password : this.state.password, - physio : this.state.physio + physio : this.state.physio, + code : this.state.code }), }) .then(res =>{ if (res.status !== 200){ - alert('Invalid username'); + if (res.status == 422){ + alert('Invalid username') + } + else if (res.status == 423){ + alert('Invalid code') + } } else{ alert('User registered'); @@ -38,6 +45,7 @@ class RegistrationScreen extends Component { this.setState({ username: "" }); this.setState({ password: "" }); + this.setState({ code: "" }); this.setState({ physio: false}); } @@ -57,7 +65,6 @@ class RegistrationScreen extends Component { }} /> - <TextInput style={styles.TextInput} placeholder="Password" @@ -68,6 +75,16 @@ class RegistrationScreen extends Component { this.setState({ password }); }} /> + <TextInput + style={styles.TextInput} + placeholder="Code" + autoCorrect={false} + secureTextEntry={true} + value = {this.state.code} + onChangeText = {code => { + this.setState({ code }); + }} + /> <Switch trackColor={{ false: "#767577", true: "#81b0ff" }} thumbColor={this.state.physio ? "#f5dd4b" : "#f4f3f4"}