Guest User

Untitled

a guest
Jan 6th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. StyleSheet,
  4. View,
  5. TextInput,
  6. Text,
  7. ActivityIndicator,
  8. Button
  9. } from 'react-native';
  10. import firebase from 'firebase'
  11.  
  12. class EmailPasswordLogin extends Component {
  13.  
  14. state = {
  15. email: "",
  16. password: "",
  17. account: {
  18. email: ""
  19. },
  20. isloading: true
  21. }
  22.  
  23. componentDidMount() {
  24. firebase.auth().onAuthStateChanged(user => {
  25. this.setState({ isloading: false })
  26. if (user && user.email) {
  27. this.setState({ account: { email: user.email } })
  28. } else {
  29. this.setState({ account: {} })
  30. }
  31. })
  32. }
  33.  
  34. login() {
  35. const { email, password } = this.state;
  36. firebase.auth().signInWithEmailAndPassword(email, password)
  37. .catch(err => {
  38. alert(`Error: ${err.message}`)
  39. })
  40. }
  41.  
  42. logout() {
  43. firebase.auth().signOut()
  44. .catch(err => {
  45. alert(`Error: ${err.message}`)
  46. });
  47. }
  48.  
  49. render() {
  50. if (this.state.isloading) {
  51. return this.renderLoading();
  52. }
  53. if (this.state.account.email) {
  54. return this.renderLoggedIn();
  55. }
  56. return this.renderLogin();
  57. }
  58.  
  59. renderLoading = () => (
  60. <View style={styles.container}>
  61. <ActivityIndicator size="large" />
  62. </View>
  63. );
  64.  
  65. renderLoggedIn = () => (
  66. <View style={styles.container}>
  67. <Text style={styles.title}>Logged In</Text>
  68. <Text style={styles.email} >{this.state.account.email}</Text>
  69. <Button onPress={() => this.logout()} title="Log Out!" />
  70. </View>
  71. );
  72.  
  73. renderLogin = () => (
  74. <View style={styles.container}>
  75. <Text style={styles.title}>Login</Text>
  76. <TextInput style={styles.input} value={this.state.email} onChangeText={email => this.setState({ email })} placeholder="Email" spellCheck={false} autoCapitalize="none" autoCorrect={true} />
  77. <TextInput style={styles.input} secureTextEntry={true} value={this.state.password} onChangeText={password => this.setState({ password })} placeholder="Password" />
  78. <Button onPress={() => this.login()} title="Login" />
  79. </View>
  80. );
  81. }
  82.  
  83. const styles = StyleSheet.create({
  84. email: {
  85. alignSelf: "center"
  86. },
  87. title: {
  88. alignSelf: "center",
  89. fontSize: 30,
  90. marginBottom: 20,
  91. fontWeight: "bold"
  92. },
  93. container: {
  94. flex: 1,
  95. padding: 20,
  96. justifyContent: 'center',
  97. },
  98. input: {
  99. height: 40,
  100. borderWidth: 1,
  101. marginBottom: 10,
  102. padding: 5,
  103. borderColor: "#dedede"
  104. }
  105. });
  106.  
  107. export default EmailPasswordLogin;
Add Comment
Please, Sign In to add comment