Skip to content

Commit 22fa49b

Browse files
committed
Push in local changes for presentation
1 parent 251b5c4 commit 22fa49b

File tree

5 files changed

+53
-23
lines changed

5 files changed

+53
-23
lines changed

RosterRN_WS.code-workspace

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
]
7+
}

app/config/routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const AuthStack = createStackNavigator({
1111
SignUp: Register,
1212
}, {
1313
headerMode: 'screen',
14+
initialRouteName: 'LogIn',
1415
});
1516

1617
const AppContainer = createAppContainer(createSwitchNavigator(

app/screens/Register.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable consistent-return */
12
import React, { Component } from 'react';
23
import { PropTypes } from 'prop-types';
34
import { Card, Input, Button } from 'react-native-elements';
@@ -6,6 +7,7 @@ import connectAlert from './../components/Alert/connectAlert';
67
import deviceStorage from './../services/deviceStorage';
78
import Container from '../components/Container';
89
import connection from './../config/connection';
10+
import postJSONContent from './../services/connectionService';
911

1012
class Register extends Component {
1113
constructor(props) {
@@ -32,28 +34,23 @@ class Register extends Component {
3234
}
3335

3436
// Move validation to custom input out from this submit function
37+
// so that the input is validated while it is written
3538
const emailReg = new RegExp('^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$', 'g');
3639
if (!emailReg.test(email)) {
3740
return this.props.alertWithType('error', 'Error', 'Email not valid');
3841
}
3942

4043
try {
4144
const URL = `${connection.SERVER_URL}:4500/user/signup`;
42-
console.log(URL);
43-
const response = await fetch(URL, {
44-
method: 'POST',
45-
headers: {
46-
Accept: 'application/json',
47-
'Content-Type': 'application/json',
48-
},
49-
body: JSON.stringify({
50-
email,
51-
password,
52-
}),
53-
});
54-
console.log(response);
55-
// deviceStorage.saveItem('id_token', response.data.jwt);
56-
// this.props.navigation.navigate('App');6
45+
const response = await fetch(URL, postJSONContent({
46+
username: email,
47+
password,
48+
}));
49+
const data = await response.json();
50+
// deviceStorage.saveItem('id_token', data.jwt);
51+
// Route back to sign-in screen for now. Later on succesful
52+
// registration sign user automatically in.
53+
this.props.navigation.navigate('LogIn');
5754
} catch (error) {
5855
console.log(error);
5956
return this.props.alertWithType('error', 'Error', `Something went wrong: ${error}`);

app/screens/SignIn.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable consistent-return */
12
import React, { Component } from 'react';
23
import { Text } from 'react-native';
34
import { PropTypes } from 'prop-types';
@@ -7,13 +8,14 @@ import Container from './../components/Container';
78
import connectAlert from './../components/Alert/connectAlert';
89
import deviceStorage from './../services/deviceStorage';
910
import { LinkText } from './../components/Text';
11+
import postJSONContent from './../services/connectionService';
1012

1113
class SignIn extends Component {
1214
constructor(props) {
1315
super(props);
1416

1517
this.state = {
16-
emailOrUsername: '',
18+
username: '',
1719
password: '',
1820
loading: false,
1921
};
@@ -22,10 +24,10 @@ class SignIn extends Component {
2224
goToSignUp = () => {
2325
this.props.navigation.navigate('SignUp');
2426
}
25-
signIn = () => {
26-
const { emailOrUsername, password, loading } = this.state;
27+
signIn = async () => {
28+
const { username, password, loading } = this.state;
2729

28-
if (emailOrUsername.length === 0) {
30+
if (username.length === 0) {
2931
return this.props.alertWithType('error', 'Error', 'Email or username is required');
3032
}
3133

@@ -36,8 +38,19 @@ class SignIn extends Component {
3638
// TODO: Get the token from back end
3739
this.setState({ loading: !loading });
3840
// return this.props.alertWithType('warn', 'Warning', 'Sign in not implemented yet');
39-
deviceStorage.saveItem('userToken', 'jwttoken123');
40-
this.props.navigation.navigate('App');
41+
try {
42+
const URL = `${connection.SERVER_URL}:4500/user/signup`;
43+
const response = await fetch(URL, postJSONContent({
44+
username,
45+
password,
46+
}));
47+
const data = await response.json();
48+
deviceStorage.saveItem('userToken', data.jwt);
49+
this.props.navigation.navigate('App');
50+
} catch (error) {
51+
// TODO: Get errortype from server. Adjust error message accordingly
52+
return this.props.alertWithType('Error', 'errortype', 'Could not sign in');
53+
}
4154
}
4255

4356
/*
@@ -54,8 +67,8 @@ class SignIn extends Component {
5467
label="Email or Username"
5568
placeholder="Enter email or username"
5669
keyboardType="email-address"
57-
onChangeText={(emailOrUsername) => this.setState({ emailOrUsername })}
58-
value={this.state.emailOrUsername}
70+
onChangeText={(username) => this.setState({ username })}
71+
value={this.state.username}
5972
leftIcon={
6073
<Icon
6174
name="envelope"

app/services/connectionService.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const postJSONContent = (args) => {
2+
return {
3+
method: 'POST',
4+
headers: {
5+
Accept: 'application/json',
6+
'Content-Type': 'application/json',
7+
},
8+
body: JSON.stringify({ ...args }),
9+
};
10+
};
11+
12+
export default postJSONContent;

0 commit comments

Comments
 (0)