Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. AppRegistry,
  4. StyleSheet,
  5. Text,
  6. TouchableHighlight,
  7. View,
  8. AsyncStorage
  9. } from 'react-native';
  10.  
  11. import firebase from "../config/firebase.js";
  12.  
  13. var t = require('tcomb-form-native');
  14.  
  15. var Form = t.form.Form;
  16.  
  17. var Login = t.struct({
  18. email: t.String,
  19. password: t.String,
  20. });
  21.  
  22. var options = {
  23. fields: {
  24. email: {
  25. error: 'Insert a valid email'
  26. }
  27. }
  28. };
  29.  
  30. export default class withFirebase extends Component {
  31.  
  32. constructor(props) {
  33. super(props);
  34.  
  35. this.state = {
  36. logged: false,
  37. }
  38.  
  39. }
  40.  
  41. onPress() {
  42. // call getValue() to get the values of the form
  43. var value = this.refs.form.getValue();
  44.  
  45. if (value) { // if validation fails, value will be null
  46.  
  47. firebase.auth()
  48. .signInWithEmailAndPassword(value.email, value.password)
  49. .then(function() {
  50.  
  51. var user = firebase.auth().currentUser;
  52. alert(user.email);
  53. this.setState({
  54. loggedIn: true,
  55. });
  56. AsyncStorage.setItem('user_data', JSON.stringify(user));
  57. }.bind(this),
  58.  
  59. function(error) {
  60. var errorCode = error.code;
  61. var errorMessage = error.message;
  62. alert(errorMessage);
  63. });
  64.  
  65. }
  66. }
  67.  
  68.  
  69. render() {
  70. return (
  71. <View style={styles.container}>
  72. <Form
  73. ref="form"
  74. type={Login}
  75. options={options}
  76. />
  77. <Text>{this.state.logged}</Text>
  78. <TouchableHighlight style={styles.button} onPress={this.onPress.bind(this)} underlayColor='#99d9f4'>
  79. <Text style={styles.buttonText}>Save</Text>
  80. </TouchableHighlight>
  81. </View>
  82. );
  83. }
  84. }
  85.  
  86. const styles = StyleSheet.create({
  87. container: {
  88. justifyContent: 'center',
  89. marginTop: 50,
  90. padding: 20,
  91. backgroundColor: '#ffffff',
  92. },
  93. buttonText: {
  94. fontSize: 18,
  95. color: 'white',
  96. alignSelf: 'center'
  97. },
  98. button: {
  99. height: 36,
  100. backgroundColor: '#48BBEC',
  101. borderColor: '#48BBEC',
  102. borderWidth: 1,
  103. borderRadius: 8,
  104. marginBottom: 10,
  105. alignSelf: 'stretch',
  106. justifyContent: 'center'
  107. }
  108. });
  109.  
  110. AppRegistry.registerComponent('withFirebase', () => withFirebase);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement