Skip to content

Commit

Permalink
Add working account creation functionality.
Browse files Browse the repository at this point in the history
Spyboticsguy committed Jan 27, 2018
1 parent 98eaf5e commit ec63772
Showing 6 changed files with 179 additions and 149 deletions.
3 changes: 0 additions & 3 deletions App.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import React from 'react';
import Expo, { SQLite } from 'expo';
import { AppRegistry, StyleSheet, Text, View, Button, Picker, TextInput, KeyboardAvoidingView } from 'react-native';
import { StackNavigator } from 'react-navigation';
import { Auth } from './src/model/Auth.js';

import HomeScreen from './src/pages/HomeScreen.js'
import LoginScreen from './src/pages/LoginScreen.js';
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -6,7 +6,9 @@
"react": "16.0.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-24.0.0.tar.gz",
"react-native-elements": "^0.19.0",
"react-native-keyboard-aware-scroll-view": "^0.4.3",
"react-native-vector-icons": "^4.5.0",
"react-navigation": "^1.0.0-beta.27"
"react-navigation": "^1.0.0-beta.27",
"tcomb-form-native": "^0.6.11"
}
}
52 changes: 49 additions & 3 deletions src/model/Auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
import { StudyBuddyDB } from './Database.js';
import t from 'tcomb-form-native';

export default class Auth {

}
const Majors = t.enums({
CS: 'Computer Science',
CM: 'Computational Media',
O: 'Other',
});

const Years = t.enums({
0: 'Freshman',
1: 'Sophomore',
2: 'Junior',
3: 'Senior',
4: '5th Year+',
});

const Email = t.subtype(t.Str, (email) => {
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return reg.test(email);
});


export const User = t.struct({
email: Email,
name: t.String,
lastName: t.String,
password: t.String,
passwordAgain: t.String,
major: Majors,
year: Years,
});

class Auth {
/**
* Wrapper for the DB getUser class.
*/
async getUser(email) {
user = await StudyBuddyDB.getUser(email);
return user;
}

/**
* Takes in a user object and creates a new account for that user.
*/
createAccount(user) {
StudyBuddyDB.addUser(user);
}
}

export const AuthObject = new Auth();
35 changes: 16 additions & 19 deletions src/model/Database.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import { SQLite } from 'expo';
import { AsyncStorage } from 'react-native';

class Database {
static users_table = 'users';
constructor(name) {
this.db = SQLite.openDatabase(name);
// TEMPORARY: drop users table before creating.
this.db.transaction(tx => {
tx.executeSql('drop table ?', [this.users_table]);
});
this.db.transaction(tx => {
tx.executeSql('create table if not exists ? (id integer primary key not null, '
+ 'firstName text, lastName text, password text, username text, major text, year text);', [this.users_table]);
});
}

/**
* This method adds a new user to the database.
*/
addUser(lastName, firstName, email, year, major, password) {
this.db.transaction(tx => {
tx.executeSql('insert into ? (firstName, lastName, password, username, major, year) VALUES (?, ?, ?, ?, ?, ?)',
[this.users_table, firstName, lastName, password, email, major, year]);
});
addUser(user) {
console.log(user);
AsyncStorage.setItem(user.email, JSON.stringify(user));
console.log(user);
}

/**
* Returns the user associated with the given email address.
* Returns null if no user or multiple users were found.
*/
async getUser(email) {
user = await AsyncStorage.getItem(email);
user = await JSON.parse(user) || null;
return user;
}

}

export const StudyBuddyDB = new Database('studybuddy.db');
export const StudyBuddyDB = new Database();
207 changes: 84 additions & 123 deletions src/pages/RegisterScreen.js
Original file line number Diff line number Diff line change
@@ -1,140 +1,101 @@
import React from 'react';
import Expo, { SQLite } from 'expo';
import { AppRegistry, StyleSheet, Text, View, Button, Picker, TextInput, KeyboardAvoidingView } from 'react-native';
import { StackNavigator } from 'react-navigation';
import { Auth } from '../model/Auth.js';
import { StyleSheet, Text, Button } from 'react-native';
import { AuthObject, User } from '../model/Auth.js';
import t from 'tcomb-form-native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';


const Form = t.form.Form;

const options = {
fields: {
name: {
placeholder: 'First name',
},
email: {
error: 'Please enter a valid email address.',
autoCapitalize: 'none',
autoCorrect: false,
},
passwordAgain: {
placeholder: 'Verify Password',
password: true,
secureTextEntry: true,
autoCapitalize: 'none',
autoCorrect: false,
},
password: {
password: true,
secureTextEntry: true,
autoCapitalize: 'none',
autoCorrect: false,
},
major : {
label: 'Major',
error: 'Please select a major.',
},
year: {
label: 'Year',
error: 'Please select a class.',
}
},
auto: 'placeholders',
};

export default class RegisterScreen extends React.Component {
static navigationOptions = {
title: 'Register',
};
constructor() {
super();
this.state = {
text_fn: '',
text_ln: '',
text_gtea: '',
text_pw: '',
text_repw: '',
year_value: '',
major_value: ''
};
}

creAcc = () => {
if ((this.state.text_fn == "")
|| (this.state.text_ln == "")
|| (this.state.text_gtea == "")
|| (this.state.text_pw == "")
|| (this.state.text_repw == "")
|| (this.state.year_value == "")
|| (this.state.major_value == "")) {
alert("Please fill in the missing sections.");
} else {
const { navigate } = this.props.navigation;
navigate('AccountCreated');
}
}

render() {
const { navigate } = this.props.navigation;
return (
<View style = {styles.container}>
<TextInput
style = {styles.textbox}
placeholder = "First Name"
onChangeText = {(text_fn) => this.setState({text_fn})}
value = {this.state.text_fn}
clearTextOnFocus = {true}
/>
<TextInput
style = {styles.textbox}
placeholder = "Last Name"
onChangeText = {(text_ln) => this.setState({text_ln})}
value = {this.state.text_ln}
clearTextOnFocus = {true}
/>
<TextInput
style = {styles.textbox}
placeholder = "GT Email Address"
onChangeText = {(text_gtea) => this.setState({text_gtea})}
value = {this.state.text_gtea}
clearTextOnFocus = {true}
/>
<TextInput
style = {styles.textbox}
placeholder = "Password"
onChangeText = {(text_pw) => this.setState({text_pw})}
value = {this.state.text_pw}
clearTextOnFocus = {true}
secureTextEntry = {true}
/>
<TextInput
style = {styles.textbox}
placeholder = "Re-enter Password"
onChangeText = {(text_repw) => this.setState({text_repw})}
value = {this.state.text_repw}
clearTextOnFocus = {true}
secureTextEntry = {true}
/>
<Picker
style = {{width: '80%'}}
selectedValue = {this.state.year_value}
onValueChange = {(itemValue, itemIndex) =>
this.setState({year_value: itemValue})}>
<Picker.Item label = "Select your year." color = '#D3D3D3' value = "" />
<Picker.Item label = "Freshman" value = "fr" />
<Picker.Item label = "Sophomore" value = "so" />
<Picker.Item label = "Junior" value = "jr" />
<Picker.Item label = "Senior/5th Year" value = "sr5" />
</Picker>

<Picker
style = {{width: '80%'}}
selectedValue = {this.state.major_value}
onValueChange = {(itemValue, itemIndex) =>
this.setState({major_value: itemValue})}>
<Picker.Item label = "Select your major." color = '#D3D3D3' value = "" />
<Picker.Item label = "Computer Science" value = "cs" />
<Picker.Item label = "Computational Media" value = "cm" />
</Picker>

<Button
static navigationOptions = {
title: 'Register',
};

constructor(props) {
super(props);
}

render() {
const { navigate } = this.props.navigation;
return (
<KeyboardAwareScrollView contentContainerStyle = {styles.container}>
<Form
ref = {c => {this._register = c;}}
type = {User}
options = {options}
/>

<Button
title="Create Account"
onPress = {this.creAcc}
/>
</View>
onPress = {this.register}
/>
</KeyboardAwareScrollView>
);
}
}
register = async () => {
const value = this._register.getValue();
const { goBack } = this.props.navigation;
if (value) {
console.log(value);
if (value.password == value.passwordAgain) {
if (await AuthObject.getUser(value.email) == null) {
AuthObject.createAccount(value);
alert("Account created!");
goBack();
} else {
alert("An account with that email already exists.");
}
} else {
alert("Please make sure your passwords match.");
}
}
}

}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
padding: '5%',
},
textbox: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
borderRadius: 3,
margin: 2,
width: '100%',
},
textboxTop: {
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
borderBottomWidth: 0,
margin: 'auto',
},
textboxBottom: {
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
margin: 'auto',
},
header: {
fontSize: 20,
},
27 changes: 27 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -3035,6 +3035,17 @@ [email protected]:
dependencies:
prop-types "^15.5.10"

react-native-iphone-x-helper@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/react-native-iphone-x-helper/-/react-native-iphone-x-helper-1.0.2.tgz#7dbca530930f7c1ce8633cc8fd13ba94102992e1"

react-native-keyboard-aware-scroll-view@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/react-native-keyboard-aware-scroll-view/-/react-native-keyboard-aware-scroll-view-0.4.3.tgz#206e197730902ce01d026910ff8409a2a363e1ac"
dependencies:
prop-types "^15.6.0"
react-native-iphone-x-helper "^1.0.1"

[email protected]:
version "0.17.1"
resolved "https://registry.yarnpkg.com/react-native-maps/-/react-native-maps-0.17.1.tgz#ab2236341fd984dac8864202ae55331bc262f60c"
@@ -3686,6 +3697,22 @@ tar@^2.2.1:
fstream "^1.0.2"
inherits "2"

tcomb-form-native@^0.6.11:
version "0.6.11"
resolved "https://registry.yarnpkg.com/tcomb-form-native/-/tcomb-form-native-0.6.11.tgz#1fda9d8fc4ed9e954e60f08d4d976ec85e04697e"
dependencies:
tcomb-validation "^3.0.0"

tcomb-validation@^3.0.0:
version "3.4.1"
resolved "https://registry.yarnpkg.com/tcomb-validation/-/tcomb-validation-3.4.1.tgz#a7696ec176ce56a081d9e019f8b732a5a8894b65"
dependencies:
tcomb "^3.0.0"

tcomb@^3.0.0:
version "3.2.24"
resolved "https://registry.yarnpkg.com/tcomb/-/tcomb-3.2.24.tgz#7f427053cc393b5997c4c3d859ca20411180887b"

[email protected]:
version "0.8.3"
resolved "https://registry.yarnpkg.com/temp/-/temp-0.8.3.tgz#e0c6bc4d26b903124410e4fed81103014dfc1f59"

0 comments on commit ec63772

Please sign in to comment.