Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {StyleSheet, Text, View} from 'react-native';
  3. import { Actions, ActionConst } from 'react-native-router-flux'
  4. import {Input, Button} from 'react-native-elements';
  5. import {decode as atob, encode as btoa} from 'base-64'
  6. import PropTypes from 'prop-types';
  7. import { connect } from 'react-redux'
  8.  
  9. import { userActionCreators } from '../redux'
  10. import Storage from '../api/Storage'
  11.  
  12. const mapStateToProps = (state) => ({
  13.   isAuthenticating: state.user.isAuthenticating,
  14.   token: state.user.token
  15. })
  16.  
  17. class Login extends Component {
  18.   static propTypes = {
  19.     dispatch: PropTypes.func.isRequired,
  20.     isAuthenticating: PropTypes.bool,
  21.     token: PropTypes.string
  22. }
  23.  
  24.   constructor(props) {
  25.     super(props)
  26.     this.state = {
  27.       user: '',
  28.       password: ''
  29.     };
  30.  
  31.     // console.log(userActionCreators);
  32.     // console.log(this.state);
  33.   }
  34.  
  35.   handleLogout = async () => {
  36.     var headers = new Headers();
  37.     headers.append("Authorization", "Basic " + btoa(this.user + ":" + this.password));
  38.     headers.append("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json");
  39.     headers.append("Content-Type", "application/json");
  40.     headers.append("Connection", "keep-alive");
  41.     headers.append("Cache-Control", "no-cache");
  42.  
  43.     fetch('http://192.168.1.8:8080/higiena/logout', {
  44.       method: 'GET',
  45.       headers: headers,
  46.       credentials: "include"
  47.     }).then(function(response) {
  48.       if (response.ok) {
  49.         console.log("LOGOUT");
  50.         console.log(response.headers);
  51.         console.log(response.blob());
  52.       }
  53.     });
  54.   }
  55.  
  56.   handleSignUp = async () => {
  57.     var headers = new Headers();
  58.     headers.append("Authorization", "Basic " + btoa(this.state.user + ":" + this.state.password));
  59.     headers.append("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json");
  60.     headers.append("Content-Type", "application/json");
  61.     headers.append("Connection", "keep-alive");
  62.     headers.append("Cache-Control", "no-cache");
  63.  
  64.     console.log(this.props.isAuthenticating);
  65.     console.log(this.props);
  66.     console.log(userActionCreators.authenticationFailure);
  67.  
  68.     fetch('http://192.168.1.8:8080/higiena', {
  69.       method: 'GET',
  70.       headers: headers,
  71.       credentials: "include"
  72.     }).then(function(response) {
  73.       if (response.ok) {
  74.         let token = btoa(this.user + ":" + this.password)
  75.         console.log("LOGIN success: " + token);
  76.         this.props.dispatch(userActionCreators.authenticationSuccess(token))
  77.       } else {
  78.         var status = response.status
  79.         console.log("LOGIN failed: " + status);
  80.         this.props.dispatch(userActionCreators.authenticationFailure(status))
  81.       }
  82.     });
  83.  
  84.   }
  85.  
  86.   render() {
  87.     return (<View style={styles.container}>
  88.       <Text>Welcome to Higiena!</Text>
  89.       <Input label="Email" leftIcon={{
  90.           type: 'font-awesome',
  91.           name: 'user'
  92.         }} onChangeText={(value) => this.setState({user: value})
  93. } placeholder="my@email.com"/>
  94.       <Input label="Password" leftIcon={{
  95.           type: 'font-awesome',
  96.           name: 'lock'
  97.         }} onChangeText={(value) => this.setState({password: value})
  98. } placeholder="p@ssw0rd123" secureTextEntry />
  99.       <Button buttonStyle={styles.button} title='Submit' onPress={this.handleSignUp}/>
  100.  
  101.       <Button buttonStyle={styles.button} title='Log Out' onPress={this.handleLogout}/>
  102.     </View>);
  103.   }
  104. }
  105.  
  106. const styles = StyleSheet.create({
  107.   container: {
  108.     flex: 1,
  109.     backgroundColor: '#fff',
  110.     alignItems: 'center',
  111.     justifyContent: 'center'
  112.   },
  113.   button: {
  114.     marginTop: 10
  115.   }
  116. });
  117.  
  118. export default connect(mapStateToProps)(Login)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement