Advertisement
Guest User

Untitled

a guest
May 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. StyleSheet,
  4. Text,
  5. View,
  6. TextInput,
  7. KeyboardAvoidingView,
  8. TouchableOpacity,
  9. AsyncStorage,
  10. ImageBackground,
  11. Dimensions,
  12. } from 'react-native';
  13.  
  14. import { StackNavigator } from 'react-navigation';
  15. import Icon from 'react-native-vector-icons/Ionicons'
  16.  
  17. const { width: WIDTH } = Dimensions.get('window')
  18.  
  19. export default class Login extends React.Component {
  20.  
  21. constructor(props){
  22. super(props);
  23. this.state = {
  24. username: '',
  25. password: '',
  26. }
  27. }
  28.  
  29. componentDidMount(){
  30. this._loadInitialState().done();
  31. }
  32.  
  33. _loadInitialState = async () => {
  34. var value = await AsyncStorage.getItem('user');
  35. if (value !== null){
  36. this.props.navigation.navigate('Profile');
  37. }
  38. }
  39.  
  40.  
  41. render() {
  42. return (
  43. //<ImageBackground source={require('././assets/splash.png')}>
  44. <KeyboardAvoidingView behavior = 'padding' style = {styles.wrapper}>
  45. <View style ={styles.container}>
  46.  
  47. <Text style= {styles.header}> LOGGA IN </Text>
  48.  
  49. <View style ={styles.inpuContainer}>
  50. <Icon name={'ios-person'} size ={28} color = {'rgba(255, 255, 255, 0.7)'}
  51. style={styles.inputIcon}/>
  52. <TextInput
  53. style={styles.textInput}
  54. placeholder = 'Username'
  55. placeholderTextColor={'rgba(255, 255, 255, 0.7)'}
  56. underlineColorAndroid='transparent'
  57. onChangeText={ (username) => this.setState({username}) }
  58.  
  59. />
  60. </View>
  61.  
  62. <View style ={styles.inpuContainer}>
  63. <Icon name={'ios-lock'} size ={28} color = {'rgba(255, 255, 255, 0.7)'}
  64. style={styles.inputIcon}/>
  65. <TextInput
  66. style={styles.textInput}
  67. placeholder = 'Password'
  68. placeholderTextColor={'rgba(255, 255, 255, 0.7)'}
  69. onChangeText={ (password) => this.setState({password}) }
  70. secureTextEntry={true} underlineColorAndroid='transparent'
  71. />
  72. <TouchableOpacity style={styles.btnEye}>
  73. <Icon name={'ios-eye'} size={26} color={'rgba(255, 255, 255, 0.7)'}/>
  74. </TouchableOpacity>
  75. </View>
  76.  
  77. <TouchableOpacity
  78. style={styles.btn}
  79. onPress={this.login}>
  80. <Text style={styles.text}>Log in</Text>
  81. </TouchableOpacity>
  82.  
  83. </View>
  84.  
  85. </KeyboardAvoidingView>
  86. // </ImageBackground>
  87. );
  88. }
  89.  
  90. login = () => {
  91.  
  92.  
  93.  
  94. fetch('http://192.168.10.228:3000/users', {
  95. method: 'POST',
  96. headers: {
  97. 'Accept': 'application/json',
  98. 'Content-Type': 'application/json',
  99. },
  100. body: JSON .stringify({
  101. username: this.state.username,
  102. password: this.state.password,
  103. })
  104. })
  105.  
  106. .then((response) => response.json())
  107. .then((res) => {
  108.  
  109. if(res.success == true){
  110. AsyncStorage.setItem('user', res.user);
  111. this.props.navigation.navigate('Profile');
  112. }
  113.  
  114. else{
  115. alert(res.message);
  116. }
  117. })
  118. .done();
  119. }
  120. }
  121.  
  122. const styles = StyleSheet.create({
  123. wrapper: {
  124. flex: 1,
  125. },
  126. backgroundContainer: {
  127. flex: 1,
  128. width: null,
  129. height: null,
  130. justifyContent: 'center',
  131. alignItems: 'center',
  132. },
  133. container: {
  134. flex: 1,
  135. alignItems: 'center',
  136. justifyContent: 'center',
  137. backgroundColor: '#2896d3',
  138. paddingLeft: 40,
  139. paddingRight: 40,
  140.  
  141. },
  142. inpuContainer: {
  143. marginTop: 10,
  144. },
  145. header: {
  146. fontSize: 24,
  147. marginBottom: 60,
  148. color: '#fff',
  149. fontWeight: 'bold',
  150. },
  151. textInput: {
  152. width: WIDTH -55,
  153. height: 45,
  154. borderRadius: 25,
  155. fontSize: 16,
  156. paddingLeft: 45,
  157. backgroundColor: 'rgba(0, 0, 0, 0.35)',
  158. color: 'rgba(255, 255, 255, 0.7)',
  159. marginHorizontal: 25,
  160. },
  161. inputIcon: {
  162. position: 'absolute',
  163. top: 8,
  164. left: 37,
  165.  
  166.  
  167. },
  168. btnEye: {
  169. position: 'absolute',
  170. top: 8,
  171. right: 37,
  172. },
  173. text: {
  174. color: 'rgba(255, 255, 255, 0.7)',
  175. fontSize: 16,
  176. textAlign: 'center',
  177. },
  178. btn: {
  179. width: WIDTH -55,
  180. height: 45,
  181. borderRadius: 25,
  182. backgroundColor: '#75D161',
  183. justifyContent: 'center',
  184. marginTop: 20,
  185. }
  186. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement