Advertisement
Guest User

Untitled

a guest
Jul 31st, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { Scene, Router } from 'react-native-router-flux';
  3. import {
  4. AppRegistry,
  5. StyleSheet
  6. } from 'react-native';
  7.  
  8. import firebaseApp from './firebase_setup';
  9.  
  10. // Components
  11. import Login from './components/user/login/login';
  12. import Home from './components/user/home/home';
  13.  
  14. // Switching scene type
  15. import FirebaseSwitch from './firebase_switch';
  16.  
  17. const styles = StyleSheet.create({
  18. navBarDefault: {
  19. backgroundColor: "#35A7FF",
  20. }
  21. });
  22.  
  23. class TextbookSwap extends Component {
  24. state = {
  25. loggedIn: false
  26. }
  27.  
  28. componentWillMount() {
  29. firebaseApp.auth().onAuthStateChanged((user) =>
  30. this.setState({ loggedIn: !!user })
  31. );
  32. }
  33.  
  34.  
  35. render() {
  36. const rootSelector = () => this.state.loggedIn ? 'appScenes' : 'authScenes';
  37. return (
  38. <Router>
  39. <Scene key="root" tabs={true} selector={rootSelector}>
  40. <Scene key="authScenes" hideNavBar={true}>
  41. <Scene key="login" component={Login} initial={true}/>
  42. </Scene>
  43. <Scene key="appScenes" hideNavBar={false} navigationBarStyle={styles.navBarDefault} titleStyle={{ color: "white" }}>
  44. <Scene key="home" component={Home} title="TextbookSwap" />
  45. </Scene>
  46. </Scene>
  47. </Router>
  48. );
  49. }
  50. }
  51.  
  52.  
  53. AppRegistry.registerComponent('TextbookSwap', () => TextbookSwap);
  54.  
  55. import React, { Component } from 'react';
  56. import { Switch } from 'react-native-router-flux';
  57.  
  58. import firebaseApp from './firebase_setup';
  59.  
  60. export default class FirebaseSwitch extends Component {
  61. state = {
  62. loggedIn: false
  63. }
  64.  
  65. componentWillMount() {
  66. firebaseApp.auth().onAuthStateChanged((user) =>
  67. this.setState({ loggedIn: !user })
  68. );
  69. }
  70.  
  71. render() {
  72. return(
  73. <Switch loggedIn={this.state.loggedIn} {...this.props} />
  74. );
  75. }
  76. }
  77.  
  78. import React, { Component } from 'react';
  79. import {
  80. AlertIOS,
  81. Dimensions,
  82. Image,
  83. ScrollView,
  84. StyleSheet,
  85. Text,
  86. TextInput,
  87. TouchableOpacity,
  88. View
  89. } from 'react-native';
  90.  
  91. import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
  92. import { Actions } from 'react-native-router-flux';
  93.  
  94. import firebaseApp from 'TextbookSwap/firebase_setup';
  95.  
  96. // Set width and height to screen dimensions
  97. const { width, height } = Dimensions.get("window");
  98.  
  99. // For Firebase Auth
  100. const auth = firebaseApp.auth();
  101.  
  102. // Styles removed for codereview
  103.  
  104. export default class Login extends Component {
  105. constructor(props) {
  106. super(props);
  107.  
  108. this.state = {
  109. email: '',
  110. password: ''
  111. }
  112. }
  113.  
  114. componentDidMount() {
  115. let user = auth.currentUser;
  116. if (user) {
  117. console.log(msg)
  118. Actions.home
  119. } else {
  120. return;
  121. }
  122. }
  123.  
  124. render() {
  125. return (
  126. <View style={styles.mainContainer}>
  127. <KeyboardAwareScrollView
  128. style={styles.scrollView}
  129. keyboardShouldPersistTaps={false}
  130. automaticallyAdjustContentInsets={true}
  131. alwaysBonceVertical={false}
  132. >
  133. <View style={styles.loginContainer}>
  134.  
  135. <Image
  136. source={require('TextbookSwap/assets/textbook.png')}
  137. style={styles.loginImage}
  138. />
  139.  
  140. <Text style={styles.headerText}>SCU TextbookSwap</Text>
  141.  
  142. <View style={styles.inputContainer}>
  143.  
  144. <TextInput
  145. style={styles.formInput}
  146. placeholder="Email"
  147. keyboardType="email-address"
  148. autoFocus={true}
  149. autoCorrect={false}
  150. autoCapitalize="none"
  151. onChangeText={(email) => this.setState({email})}
  152. />
  153.  
  154. <TextInput
  155. style={styles.formInput}
  156. secureTextEntry={true}
  157. placeholder="Password"
  158. autoCorrect={false}
  159. autoCapitalize="none"
  160. onChangeText={(password) => this.setState({password})}
  161. />
  162.  
  163. <TouchableOpacity
  164. style={styles.loginButton}
  165. onPress={this._logInUser.bind(this)}
  166. >
  167. <Text style={styles.loginButtonText}>Log In</Text>
  168. </TouchableOpacity>
  169.  
  170. <TouchableOpacity>
  171. <Text style={styles.toSignupButton}>Dont have an account? Create one!</Text>
  172. </TouchableOpacity>
  173.  
  174. </View>
  175. </View>
  176.  
  177. <View style={styles.footer}>
  178. <Text style={styles.footerText}>
  179. By signing up, I agree to TextbookSwap's <Text style={styles.footerActionText}>Terms of Service</Text> and <Text style={styles.footerActionText}>Privacy Policy</Text>.
  180. </Text>
  181. </View>
  182. </KeyboardAwareScrollView>
  183. </View>
  184. );
  185. }
  186.  
  187. _logInUser() {
  188. let email = this.state.email;
  189. let password = this.state.password;
  190.  
  191. auth.signInWithEmailAndPassword(email, password)
  192. .then(Actions.home)
  193. .catch((error) => {
  194. switch(error.code) {
  195. case "auth/wrong-password":
  196. AlertIOS.alert('Uh oh!', 'Invalid password! Please try again.');
  197. break;
  198.  
  199. case "auth/invalid-email":
  200. AlertIOS.alert('Uh oh!', 'Invalid email! Please try again.');
  201. break;
  202.  
  203. case "auth/user-not-found":
  204. AlertIOS.alert('Uh oh!', 'Please check your credentials and try again');
  205. break;
  206. }
  207. });
  208. }
  209. }
  210.  
  211. import React, { Component } from 'react';
  212. import { Actions } from 'react-native-router-flux';
  213. import {
  214. AlertIOS,
  215. Dimensions,
  216. StyleSheet,
  217. Text,
  218. TouchableOpacity,
  219. View
  220. } from 'react-native';
  221.  
  222. import firebaseApp from 'TextbookSwap/firebase_setup';
  223.  
  224. const { width, height } = Dimensions.get("window");
  225. const auth = firebaseApp.auth();
  226.  
  227. // Styles removed for codereview
  228.  
  229. export default class Home extends Component {
  230. render() {
  231. let user = auth.currentUser;
  232.  
  233. return (
  234. <View>
  235. <View style={styles.homeContainer}>
  236. <TouchableOpacity style={styles.logOutButton} onPress={this._signUserOut}>
  237. <Text style={styles.logOutButtonText}>Log out: {user.email}</Text>
  238. </TouchableOpacity>
  239. </View>
  240. </View>
  241. );
  242.  
  243.  
  244. }
  245. _signUserOut() {
  246. auth.signOut().then(() => {
  247. console.log("User signed out");
  248. Actions.login
  249. }, (error) => {
  250. AlertIOS.alert(
  251. `${error.code}`,
  252. `${error.message}`
  253. );
  254. });
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement