Skip to content
Snippets Groups Projects
Commit 1557e4b4 authored by ric's avatar ric
Browse files

updating

parent 9d95ce01
No related branches found
No related tags found
No related merge requests found
...@@ -2,9 +2,10 @@ const mongoose = require('mongoose'); ...@@ -2,9 +2,10 @@ const mongoose = require('mongoose');
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const UserSchema = new mongoose.Schema({ const UserSchema = new mongoose.Schema({
username:String, username: String,
password: String, password: String,
physio: Boolean physio: Boolean,
code: String
}) })
//Here we'll do the encryption //Here we'll do the encryption
......
...@@ -23,6 +23,7 @@ router.post('/signup', (req, res)=>{ ...@@ -23,6 +23,7 @@ router.post('/signup', (req, res)=>{
const username = req.body.username; const username = req.body.username;
const password = req.body.password; const password = req.body.password;
const physio = req.body.physio; const physio = req.body.physio;
const code = req.body.code;
userFromDb.findOne({'username':username}, function(err, result){ userFromDb.findOne({'username':username}, function(err, result){
if(err){ console.log("Error with the database");}; if(err){ console.log("Error with the database");};
...@@ -31,9 +32,25 @@ router.post('/signup', (req, res)=>{ ...@@ -31,9 +32,25 @@ router.post('/signup', (req, res)=>{
return res.status(422).send('Invalid username') return res.status(422).send('Invalid username')
} }
else{ else{
const user = new userFromDb({username:username, password:password, physio:physio}); if (physio){
user.save(); userFromDb.findOne({'code':code}, function(err, result){
res.status(200).send('User registered'); 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)=>{ ...@@ -55,8 +72,12 @@ router.post('/signin', (req, res)=>{
if (err) throw err; if (err) throw err;
//console.log(password, isMatch); //console.log(password, isMatch);
if (isMatch){ if (isMatch){
res.status(200).send('ok'); if (result.physio){
console.log(result.physio); res.status(201).send('Physio');
}
else {
res.status(200).send(JSON.stringify({username: "Luca"}));
}
}else{ }else{
return res.status(422).send('Invalid username or password'); return res.status(422).send('Invalid username or password');
} }
......
...@@ -12,7 +12,6 @@ class LoginScreen extends Component { ...@@ -12,7 +12,6 @@ class LoginScreen extends Component {
submitLogin() { submitLogin() {
const bool = true;
//here we're going to post the username and password inserted in the //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 //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 //in the server that will response with status:200 if the credentials are in the
...@@ -27,18 +26,21 @@ class LoginScreen extends Component { ...@@ -27,18 +26,21 @@ class LoginScreen extends Component {
password: this.state.password password: this.state.password
}), }),
}) //here we handle the status response of the server }) //here we handle the status response of the server
/*.then(res => res.json())
.then((data => {
alert(data.username)
}))*/
.then(res => { .then(res => {
if (res.status !== 200) { if (res.status == 200) {
alert('Invalid username or password'); alert('Succesful login')
this.props.navigation.navigate('Chat')
} }
else { else if (res.status == 201){
alert('Succesful login') alert('Succesful login')
if (bool){ this.props.navigation.navigate('test')
this.props.navigation.navigate('test') }
} else {
else{ alert('Unsuccesful login')
this.props.navigation.navigate('Chat')
}
} }
}) })
//here we set again username and password as blank //here we set again username and password as blank
......
...@@ -7,7 +7,8 @@ class RegistrationScreen extends Component { ...@@ -7,7 +7,8 @@ class RegistrationScreen extends Component {
this.state = { this.state = {
username: "", username: "",
password: "", password: "",
physio: false physio: false,
code: ""
}; };
} }
...@@ -23,12 +24,18 @@ class RegistrationScreen extends Component { ...@@ -23,12 +24,18 @@ class RegistrationScreen extends Component {
body: JSON.stringify({ body: JSON.stringify({
username : this.state.username, username : this.state.username,
password : this.state.password, password : this.state.password,
physio : this.state.physio physio : this.state.physio,
code : this.state.code
}), }),
}) })
.then(res =>{ .then(res =>{
if (res.status !== 200){ if (res.status !== 200){
alert('Invalid username'); if (res.status == 422){
alert('Invalid username')
}
else if (res.status == 423){
alert('Invalid code')
}
} }
else{ else{
alert('User registered'); alert('User registered');
...@@ -38,6 +45,7 @@ class RegistrationScreen extends Component { ...@@ -38,6 +45,7 @@ class RegistrationScreen extends Component {
this.setState({ username: "" }); this.setState({ username: "" });
this.setState({ password: "" }); this.setState({ password: "" });
this.setState({ code: "" });
this.setState({ physio: false}); this.setState({ physio: false});
} }
...@@ -57,7 +65,6 @@ class RegistrationScreen extends Component { ...@@ -57,7 +65,6 @@ class RegistrationScreen extends Component {
}} }}
/> />
<TextInput <TextInput
style={styles.TextInput} style={styles.TextInput}
placeholder="Password" placeholder="Password"
...@@ -68,6 +75,16 @@ class RegistrationScreen extends Component { ...@@ -68,6 +75,16 @@ class RegistrationScreen extends Component {
this.setState({ password }); this.setState({ password });
}} }}
/> />
<TextInput
style={styles.TextInput}
placeholder="Code"
autoCorrect={false}
secureTextEntry={true}
value = {this.state.code}
onChangeText = {code => {
this.setState({ code });
}}
/>
<Switch <Switch
trackColor={{ false: "#767577", true: "#81b0ff" }} trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={this.state.physio ? "#f5dd4b" : "#f4f3f4"} thumbColor={this.state.physio ? "#f5dd4b" : "#f4f3f4"}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment