Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. import React from 'react';
  2. import { StyleSheet, Text, View, AppRegistry, TouchableOpacity, TextInput, KeyboardAvoidingView, Image, StatusBar } from 'react-native';
  3. import axios from 'axios';
  4.  
  5. export default class Login extends React.Component {
  6. static navigationOptions = {
  7. title: 'Login',
  8. header: null,
  9. };
  10. constructor() {
  11. super();
  12. this.state = {
  13. username: '',
  14. password: '',
  15. errorMessage: false,
  16. }
  17. }
  18. logIn = () => {
  19. this.setState({errorMessage: false})
  20. let hostname = "10.193.238.104"; //Alec's IP
  21. let bottleEndpt = "http://" + hostname + ":3000/api/login";
  22. body = {
  23. "username": this.state.username,
  24. "password": this.state.password,
  25. }
  26. axios.post(bottleEndpt, body)
  27. .then((response) => {
  28. this.props.navigation.navigate('Home', {data: response.data});
  29. })
  30. .catch((error) => {
  31. console.log('Error', JSON.stringify(error));
  32. this.setState({errorMessage: true})
  33. });
  34. };
  35. render() {
  36. const { navigate } = this.props.navigation;
  37. return (
  38. <View style={styles.container}>
  39. <View style={styles.logoContainer}>
  40. <Image style={styles.logo}
  41. source={require('../assets/paper.png')}
  42. />
  43. </View>
  44. <KeyboardAvoidingView behavior="padding">
  45. <View style={styles.formContainer}>
  46. <TextInput style={styles.input}
  47. placeholder="Username"
  48. returnKeyType="next"
  49. onSubmitEditing={() => this.passwordInput.focus()}
  50. onChangeText={(text)=>this.setState({username: text})}
  51. autoCorrect={false}
  52. />
  53. <TextInput style={styles.input}
  54. placeholder="Password"
  55. secureTextEntry
  56. returnKeyType="done"
  57. ref={(input) => this.passwordInput = input}
  58. onChangeText={(text)=>this.setState({password: text})}
  59. />
  60. </View>
  61. <TouchableOpacity style={styles.loginButtonContainer} onPress={this.logIn}>
  62. <Text style={styles.buttonText}>Login</Text>
  63. </TouchableOpacity>
  64. <View style={styles.signUpWrapper}>
  65. {this.state.errorMessage &&
  66. <Text style={{color: 'red', paddingRight: 100,}}>Error Logging In!</Text>
  67. }
  68. <Text style={styles.signUpText}>Not a user?</Text>
  69. <TouchableOpacity style={styles.signUpButtonContainer} onPress={() => navigate('SignUp')}>
  70. <Text style={styles.buttonText}>Sign Up</Text>
  71. </TouchableOpacity>
  72. </View>
  73. </KeyboardAvoidingView>
  74. </View>
  75. );
  76. }
  77. }
  78.  
  79. const styles = StyleSheet.create({
  80. container: {
  81. flex: 1,
  82. backgroundColor: '#96ceb4',
  83. padding: 20,
  84. },
  85. logoContainer: {
  86. alignItems: 'center',
  87. flexGrow: 1,
  88. justifyContent: 'center',
  89. },
  90. logo: {
  91. width: 150,
  92. height: 150,
  93. },
  94. loginButtonContainer: {
  95. backgroundColor: '#74BE9B',
  96. paddingVertical: 15,
  97. marginBottom: 10,
  98. },
  99. signUpWrapper: {
  100. alignSelf: 'flex-end',
  101. flexDirection: 'row',
  102. alignItems: 'center',
  103. },
  104. signUpText: {
  105. color: '#fff',
  106. },
  107. signUpButtonContainer: {
  108. backgroundColor: '#74BE9B',
  109. paddingVertical: 15,
  110. width: 65,
  111. marginLeft: 10,
  112. },
  113. buttonText: {
  114. textAlign: 'center',
  115. color: '#fff',
  116. fontWeight: '700',
  117. },
  118. input: {
  119. height: 40,
  120. backgroundColor: 'rgba(255, 255, 255, 0.7)',
  121. paddingHorizontal: 10,
  122. marginBottom: 10,
  123. },
  124. });
  125.  
  126. AppRegistry.registerComponent('Login', () => Login);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement