diff --git a/backend/routes/authRoutes.js b/backend/routes/authRoutes.js index 7f26197ba9c3e624a08a07c8dba9acdd3ea3fafe..d34fc2ff9b90265655a73260ef7adc5ce5f731af 100644 --- a/backend/routes/authRoutes.js +++ b/backend/routes/authRoutes.js @@ -4,6 +4,18 @@ const mongoose = require('mongoose'); const router = express.Router(); const userFromDb = require('../models/userModel'); +router.post('/test', (req, res)=>{ + const username = req.body.username; + const password = req.body.password; + console.log( username + ' ' + password ); + if (username == 'test'){ + return res.status(200).send('Valid username'); + } + else{ + return res.status(422).send('Invalid username'); + } +}) + //register post router.post('/signup', (req, res)=>{ console.log(req.body); @@ -20,7 +32,7 @@ router.post('/signup', (req, res)=>{ else{ const user = new userFromDb({username:username, password:password}); user.save(); - res.send('User registered'); + res.status(200).send('User registered'); } }) }) @@ -42,7 +54,7 @@ router.post('/signin', (req, res)=>{ if (err) throw err; //console.log(password, isMatch); if (isMatch){ - res.send('Login successful'); + res.status(200).send('Login successful'); }else{ return res.status(422).send('Invalid username or password'); } diff --git a/mobile/App.js b/mobile/App.js index e49001ad813bd19bdcc2332f60258b620762754d..2625a9ee863ce315633fb81b5e5ee6a17007e0d0 100644 --- a/mobile/App.js +++ b/mobile/App.js @@ -1,8 +1,9 @@ import React from 'react'; import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; +import LoginScreen from './Screens/loginScreen' import HomeScreen from './Screens/homeScreen' -import SecondScreen from './Screens/secondScreen' +import RegistrationScreen from './Screens/registrationScreen' const Stack = createNativeStackNavigator(); @@ -14,12 +15,16 @@ class App extends React.Component { <NavigationContainer> <Stack.Navigator> <Stack.Screen - name="Home" - component={HomeScreen} + name="Login" + component={LoginScreen} /> <Stack.Screen - name="Second" - component={SecondScreen} + name="Home" + component={HomeScreen} + /> + <Stack.Screen + name="Registration" + component={RegistrationScreen} /> </Stack.Navigator> </NavigationContainer> diff --git a/mobile/Screens/homeScreen.js b/mobile/Screens/homeScreen.js index 5bb1cff2c31c1deda561e035ffd785dabd7d6baa..8ef997b22ebaa0ee5003f9296c857353f40d9f50 100644 --- a/mobile/Screens/homeScreen.js +++ b/mobile/Screens/homeScreen.js @@ -1,19 +1,12 @@ import React, {Component} from 'react'; import { StyleSheet, Text, View, Button, TextInput } from 'react-native'; -import io from "socket.io-client"; +import {io} from "socket.io-client"; //socket.io-client class HomeScreen extends Component { - constructor(props) { - super(props); - this.state = { - chatMessage: "", - chatMessages: [] - }; - } - componentDidMount() { - this.socket = io("http://0.0.0.0:3000", { - transports: ['websocket'] //this line is fundamental + componentDidMount() { + const socket = io("http://192.168.178.92:3000", { + transports: ['websocket'] //this line is fundamental }); } @@ -42,16 +35,11 @@ class HomeScreen extends Component { <TextInput style={{ height: 40, borderWidth: 2 }} autoCorrect={false} - value={this.state.chatMessage} - onSubmitEditing={() => this.submitChatMessage()} - onChangeText={chatMessage => { - this.setState({ chatMessage }); - }} + /> - {chatMessages} - </View> - ); - } + </View> + ); + } } // ... diff --git a/mobile/Screens/loginScreen.js b/mobile/Screens/loginScreen.js new file mode 100644 index 0000000000000000000000000000000000000000..7515fe161f206f318436bee1e1c4365c50e56eae --- /dev/null +++ b/mobile/Screens/loginScreen.js @@ -0,0 +1,103 @@ +import React, {Component} from 'react'; +import { StyleSheet, Text, View, Button, TextInput } from 'react-native'; + +class LoginScreen extends Component { + constructor(props) { + super(props); + this.state = { + username: "", + password: "" + }; + } + + submitLogin() { + //try to post something to the server + + fetch('http://192.168.178.92:3000/signin', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + username : this.state.username, + password : this.state.password + }), + }) + .then(res =>{ + if (res.status !== 200){ + alert('Invalid username or password'); + } + else{ + alert('Succesful login') + this.props.navigation.navigate('Home') + } + }) + + this.setState({ username: "" }); + this.setState({ password: "" }); + } + + render() { + + return ( + <View style={styles.container}> + <Text>LOGIN</Text> + + <TextInput + style={styles.TextInput} + placeholder = "Username" + autoCorrect = {false} + value = {this.state.username} + onChangeText = {username => { + this.setState({ username }); + }} + /> + + + <TextInput + style={styles.TextInput} + placeholder="Password" + autoCorrect={false} + secureTextEntry={true} + value = {this.state.password} + onChangeText = {password => { + this.setState({ password }); + }} + /> + + <Button + title="Login" + onPress={() => + this.submitLogin() + } + /> + {/* this will be the button for the registration page */} + <Button + title="Go to Registration" + onPress={() => { + this.props.navigation.navigate('Registration') + } + } + /> + </View> + ); + } +} + +// ... +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#fff', + alignItems: 'center', + justifyContent: 'center', + }, + TextInput: { + height: 40, + width: 200, + borderWidth: 2, + padding:10 + } + }); + +export default LoginScreen; \ No newline at end of file diff --git a/mobile/Screens/registrationScreen.js b/mobile/Screens/registrationScreen.js new file mode 100644 index 0000000000000000000000000000000000000000..80fdbb4acf4ec3eb6d75486010dfec9b692f6f7a --- /dev/null +++ b/mobile/Screens/registrationScreen.js @@ -0,0 +1,102 @@ +import React, {Component} from 'react'; +import { StyleSheet, Text, View, Button, TextInput } from 'react-native'; + +class RegistrationScreen extends Component { + constructor(props) { + super(props); + this.state = { + username: "", + password: "" + }; + } + + submitRegistration() { + //try to post something to the server + + fetch('http://192.168.178.92:3000/signup', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + username : this.state.username, + password : this.state.password + }), + }) + .then(res =>{ + if (res.status !== 200){ + alert('Invalid username'); + } + else{ + alert('User registered'); + this.props.navigation.navigate('Login'); + } + }) + + this.setState({ username: "" }); + this.setState({ password: "" }); + } + + render() { + + return ( + <View style={styles.container}> + <Text>REGISTRATION</Text> + + <TextInput + style={styles.TextInput} + placeholder = "Username" + autoCorrect = {false} + value = {this.state.username} + onChangeText = {username => { + this.setState({ username }); + }} + /> + + + <TextInput + style={styles.TextInput} + placeholder="Password" + autoCorrect={false} + secureTextEntry={true} + value = {this.state.password} + onChangeText = {password => { + this.setState({ password }); + }} + /> + + <Button + title="Register" + onPress={() => + this.submitRegistration() + } + /> + {/* this will be the button for the registration page */} + <Button + title="Go to Login" + onPress={() => + this.props.navigation.navigate('Login') + } + /> + </View> + ); + } +} + +// ... +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#fff', + alignItems: 'center', + justifyContent: 'center', + }, + TextInput: { + height: 40, + width: 200, + borderWidth: 2, + padding:10 + } + }); + +export default RegistrationScreen; \ No newline at end of file