-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateAccount.js
More file actions
210 lines (191 loc) · 6.05 KB
/
Copy pathCreateAccount.js
File metadata and controls
210 lines (191 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, { Component } from 'react';
import { StyleSheet, Text, View, TextInput, TouchableOpacity, ScrollView, TouchableWithoutFeedback, Keyboard } from 'react-native';
import * as firebase from 'firebase';
import NavigationBar from 'react-native-navbar';
import { Analytics, ScreenHit, Event } from 'expo-analytics';
class CreateAccount extends React.Component {
constructor() {
super();
this.state = {
email: "",
password: "",
emailEmpty: true,
passwordEmpty: true,
}
global.analytics.hit(new ScreenHit('CreateAccount'))
.then(() => console.log("success"))
.catch(e => console.log(e.message));
}
render() {
const { params } = this.props.navigation.state;
return (
<View style={styles.navbar}>
<NavigationBar
leftButton = {{
title: 'Cancel',
handler: () => {this.props.navigation.goBack();},
}}
/>
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
<View style={styles.container}>
<Text style={styles.titleText}>Welcome To</Text>
<Text style={styles.titleText}>Momento</Text>
<Text>Password needs to be at least 6 characters</Text>
<TextInput
value={this.state.email}
keyboardType = 'email-address'
onChangeText={(email) => this.setState({ email: email, emailEmpty: email.length == 0 })}
placeholder='email'
placeholderTextColor = 'red'
autoCapitalize = 'none'
style={[this.state.emailEmpty ? styles.invalidInput : styles.validInput]}
/>
<TextInput
value={this.state.password}
onChangeText={(password) => this.setState({ password: password, passwordEmpty: password.length == 0 })}
placeholder={'password'}
secureTextEntry={true}
placeholderTextColor = 'red'
style={[this.state.passwordEmpty ? styles.invalidInput : styles.validInput]}
/>
<TouchableOpacity
style={styles.button}
onPress={this.onCreateAccount.bind(this)}
>
<Text style={styles.buttonText}>Create Account</Text>
</TouchableOpacity>
</View>
</TouchableWithoutFeedback>
</View>
);
}
onCreateAccount() {
if (this.state.email.length != 0 && this.state.password.length != 0) {
global.analytics.event(new Event('Account', 'Create', this.state.email))
.then(() => console.log("success"))
.catch(e => console.log(e.message));
this.createAccount(this.state.email, this.state.password)
//Check if user's email already exists in database, if not create it and navigate to InviteUser.
//If so, change password to user's chosen password, navigate straight to 'Home'
}
}
//Create User Accounts
createAccount(email, password){
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
var myRef = firebase.database().ref('/users/');
myRef.once('value').then(function(snapshot)
{
var data = snapshot.val();
if(data[this.emailToHeader(email)] !== undefined) {
}
//If the user does not exist, create them on the DataBase!
else {
this.addUser(this.emailToHeader(email), "blue");
//Create them a default momento
this.createMomento("default" + this.emailToHeader(email));
}
this.signIn(email, password);
Promise.all(this.getMomentoName(this.emailToHeader(email)).then(function(snapshots) {
this.props.navigation.navigate('Home');
}.bind(this)))
}.bind(this))
}.bind(this),
function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
});
};
addUser(userName, color) {
var myRef = firebase.database().ref('/users');
defaultUserName = "default" + userName;
myRef.update({[userName]: {color: color, momentos: {defaultMomento: defaultUserName}}});
}
createMomento(momentoName) {
var myRef = firebase.database().ref('/data');
myRef.update({[momentoName]: {0: 0}});
}
signIn(email, password) {
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
});
}
getMomentoName(userName) {
var myRef = firebase.database().ref('/users/' + userName +'/momentos');
return myRef.once('value').then(function(snapshot){
var peep = snapshot.val();
return this.getUserMomentos(peep)
}.bind(this))
}
getUserMomentos(momentoName) {
global.allTimelineNames = Object.values(momentoName);
singleMoment = momentoName[Object.keys(momentoName)[0]];
global.timelineName = singleMoment;
}
emailToHeader(email) {
return email.replace(".","*-*");
}
headerToEmail(header) {
return header.replace("*-*",".");
}
}
const styles = StyleSheet.create({
navbar: {
flex: 1,
backgroundColor: 'white',
paddingHorizontal: 10,
paddingTop: 28,
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
},
titleText:{
fontFamily: 'Baskerville',
fontSize: 50,
alignItems: 'center',
justifyContent: 'center',
},
button: {
alignItems: 'center',
backgroundColor: 'powderblue',
width: 200,
height: 44,
padding: 10,
borderWidth: 1,
borderColor: 'white',
borderRadius: 25,
marginBottom: 10,
},
buttonText:{
fontFamily: 'Baskerville',
fontSize: 20,
alignItems: 'center',
justifyContent: 'center',
},
validInput: {
width: 200,
fontFamily: 'Baskerville',
fontSize: 20,
height: 44,
padding: 10,
borderWidth: 1,
borderColor: 'black',
marginVertical: 10,
},
invalidInput: {
width: 200,
fontFamily: 'Baskerville',
fontSize: 20,
height: 44,
padding: 10,
borderWidth: 1,
borderColor: 'red',
marginVertical: 10,
}
});
export default CreateAccount;