Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import {
  3. Text,
  4. View,
  5. Picker,
  6. Button,
  7. TextInput,
  8. FlatList,
  9. ActionSheetIOS,
  10. Image
  11. } from "react-native";
  12. import { ImagePicker, Permissions, Location, MapView, Constants } from "expo";
  13. import { createStackNavigator } from "react-navigation";
  14. import * as firebase from "firebase"; //npm install --save firebase
  15. import { Card, FormLabel, FormInput } from "react-native-elements";
  16. import uuid from "uuid";
  17. //mi linko al db (devi usare il link al tuo db)
  18. var config = {
  19. apiKey: "AIzaSyA92F_Ds5jEUEKlQW_KD6793cPVbwvaXyw",
  20. authDomain: "todolist-38f45.firebaseapp.com",
  21. databaseURL: "https://todolist-38f45.firebaseio.com",
  22. projectId: "todolist-38f45",
  23. storageBucket: "todolist-38f45.appspot.com",
  24. messagingSenderId: "73445961295"
  25. };
  26. firebase.initializeApp(config);
  27. //--
  28.  
  29. const TINT_COLOR = "rgb(4, 159, 239)";
  30. class login extends Component {
  31. static navigationOptions = {
  32. title: "Login"
  33. };
  34. state = {
  35. isLoading: false,
  36. email: "nuovo@gmail.com",
  37. password: "pippo1234",
  38. error: ""
  39. };
  40.  
  41. _login = () => {
  42. this.setState({ isLoading: true });
  43. firebase
  44. .auth()
  45. .signInWithEmailAndPassword(this.state.email, this.state.password)
  46. .then(user => {
  47. this.setState({ isLoading: false });
  48. console.log("LOGIN AVVENUTO CON SUCCESSO");
  49. this.props.navigation.navigate("Load"); //gio devi crearti lo stack navigator con questa screen
  50. })
  51. .catch(error => {
  52. this.setState({ isLoading: false, error: error.message });
  53. });
  54. };
  55.  
  56. _signUp = () => {
  57. this.setState({ isLoading: true });
  58. firebase
  59. .auth()
  60. .createUserWithEmailAndPassword(this.state.email, this.state.password)
  61. .then(user => {
  62. this.setState({ isLoading: false });
  63. console.log("REGISTRAZIONE AVVENTUTA CON SUCCESSO");
  64. this.props.navigation.navigate("Load");
  65. })
  66. .catch(error => {
  67. this.setState({ isLoading: false, error: error.message });
  68. //alert(error.message);
  69. });
  70. };
  71.  
  72. renderLoginOrSpinner() {
  73. return (
  74. <View style={{ justifyContent: "space-between", height: "40%" }}>
  75. <Button
  76. loading={this.state.isLoading}
  77. raised
  78. backgroundColor={TINT_COLOR}
  79. title="Login"
  80. onPress={this._login}
  81. />
  82. <Button
  83. raised
  84. loading={this.state.isLoading}
  85. backgroundColor={TINT_COLOR}
  86. title="Register"
  87. onPress={this._signUp}
  88. />
  89. </View>
  90. );
  91. }
  92.  
  93. render() {
  94. return (
  95. <View>
  96. <Card>
  97. <FormLabel>E-mail</FormLabel>
  98. <FormInput
  99. label="E-mail"
  100. placeholder="enter a valid e-mail"
  101. onChangeText={text => this.setState({ email: text })}
  102. //value={this.state.email}
  103. />
  104.  
  105. <FormLabel>Password</FormLabel>
  106. <FormInput
  107. secureTextEntry
  108. label="Password"
  109. placeholder="your password"
  110. onChangeText={text => this.setState({ password: text })}
  111. //value={this.state.password}
  112. />
  113.  
  114. {this.renderLoginOrSpinner()}
  115. <Text>{this.state.error}</Text>
  116. </Card>
  117. </View>
  118. );
  119. }
  120. }
  121. class upload extends React.Component {
  122. state = {
  123. uri: "https://www.google.com/images/branding/product/ico/googleg_lodp.ico"
  124. };
  125. loadImage = async localURI => {
  126. const uid = firebase.auth().currentUser.uid;
  127. const response = await fetch(localURI);
  128. console.log("response:", response);
  129. const blob = await response.blob();
  130. console.log("blob:", blob);
  131. console.log("uid:", uuid);
  132. console.log("LOCAL URI: ", localURI);
  133. let pathImg = uuid.v4();
  134. const ref = firebase
  135. .storage()
  136. .ref()
  137. .child(uid + "/" + pathImg);
  138. console.log("ref", ref);
  139. const uploadStatus = await ref.put(blob);
  140. console.log(uploadStatus);
  141. const downloadURL = await uploadStatus.ref.getDownloadURL();
  142. console.log(downloadURL);
  143. this.props.navigation.setParams({ loadedList: true });
  144. console.log("SONO QUI");
  145. return downloadURL;
  146. };
  147. render() {
  148. return (
  149. <View>
  150. <Button
  151. title="Load image to firebase database"
  152. onPress={() => this.loadImage(this.state.uri)}
  153. />
  154. </View>
  155. );
  156. }
  157. }
  158. const RootStack1 = createStackNavigator(
  159. {
  160. Login: {
  161. screen: login
  162. },
  163. Load: {
  164. screen: upload
  165. }
  166. },
  167. {
  168. initialRouteName: "Login" /*posso mettere tutte le opzioni dell'header*/
  169. }
  170. );
  171.  
  172. class ExampleLoginAndUpload extends React.Component {
  173. render() {
  174. return <RootStack1 />;
  175. }
  176. }
  177. export default ExampleLoginAndUpload;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement