Advertisement
Guest User

Sha256-2

a guest
Jul 2nd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react';
  2. import {
  3.     StyleSheet,
  4.     View,
  5.     Button,
  6.     TextInput,
  7.     Image,
  8.     TouchableOpacity,
  9.     Text,
  10.     ActivityIndicator,
  11.     ToastAndroid,
  12.     StatusBar,
  13.     Alert,
  14.     AsyncStorage,
  15.     AppRegistry,
  16.     Platform
  17. } from 'react-native'
  18. import {RequestHelper, setInitialRoute} from '../property/Helpers';
  19. import {StackNavigator, NavigationActions} from 'react-navigation';
  20. import FontAwesome from 'react-native-vector-icons/FontAwesome';
  21. import placeholder from 'rn-placeholder';
  22. import {connect} from 'react-redux';
  23. import HomeScreen from './HomeMenuScreen';
  24. import {saveToken} from '../store/action/UserAction';
  25. import RegisterScreen from './RegistrationScreen'
  26. import {QTextInput, QButtonImage, QButtonText} from '../component/elements';
  27. import Entypo from 'react-native-vector-icons/Entypo';
  28. import { sha256 } from 'react-native-sha256';
  29.  
  30. class LoginScreen extends Component {
  31.     static navigationOptions = {
  32.         header: null
  33.     };
  34.  
  35.     constructor(props) {
  36.         super(props);
  37.         this.props = props;
  38.         this.state = {
  39.             username: null,
  40.             password: null,
  41.             isReady: false,
  42.             token: null,
  43.             hidePassword:true,
  44.             // instruksi: false,
  45.             loading: false
  46.         };
  47.         this.inputFile = {};
  48.         this.instruksi = this
  49.             .instruksi
  50.             .bind(this);
  51.     }
  52.  
  53.     componentDidMount = async() => {
  54.         const v = await AsyncStorage.getItem("accessToken");
  55.         if (v != undefined) {
  56.             this.setState({token_storage: v});
  57.             setInitialRoute('Home', this.props);
  58.         };
  59.     };
  60.  
  61.     // onChange = () => {     console.log(this.props); this.props.LoginRequest(true)
  62.     //     console.log(this.props); }
  63.  
  64.     handleUserName = (text) => {
  65.         this.setState({username: text})
  66.     };
  67.  
  68.     handlePassword = (text) => {
  69.         this.setState({password: text})
  70.     };
  71.  
  72.     focusNext(id) {
  73.         this
  74.             .inputFile[id]
  75.             .focus();
  76.     };
  77.  
  78.     instruksi() {
  79.         this.setState({loading: true});
  80.         setTimeout(() => {
  81.             this.setState({instruksi: true, loading: false})
  82.         }, 2000)
  83.     }
  84.  
  85.     managePasswordVisibility = () =>{
  86.         this.setState({hidePassword: !this.state.hidePassword});
  87.     }
  88.  
  89.     //Ecnryp MD5
  90.     hashmd5(textin) {
  91.         md5 = require('js-md5');
  92.         md5(textin);
  93.         var hash = md5.create();
  94.         hash.update(textin);
  95.         return hash.hex();
  96.     }
  97.  
  98.     //Encryp SHA256
  99.     Sha256(){
  100.     sha256("Test").then( hash => {
  101.         console.log(hash);
  102.     })}
  103.  
  104.     SomethisReing = async() => {
  105.         if (this.state.username == null || this.state.password == null || this.state.username.trim() == "" || this.state.password.trim() == "") {
  106.             Alert.alert("Error", "Please fill a valid username and password!");
  107.         } else {
  108.             if (!this.state.isReady) {
  109.                 this.setState({isReady: true});
  110.                 let bodyParameter = {
  111.                     email: this.state.username,
  112.                     password: this.state.password
  113.                 };
  114.                 console.log("username = " + this.state.username);
  115.                 console.log("password = " + this.state.password);
  116.                 var password_enkrip = this.Sha256(this.state.password);
  117.                 console.log("password enkrip = " + password_enkrip);
  118.                 try {
  119.                     var data = await RequestHelper('https://reqres.in/api/login', 'POST', bodyParameter);
  120.                     this.setState({isReady: false});
  121.                     if (data.error != undefined) {
  122.                         Alert.alert("Error", data.error);
  123.                     } else if (data.token != undefined) {
  124.                         this.setState({token: data.token});
  125.                         // AsyncStorage.setItem("accessToken", data.token); setInitialRoute('Home',
  126.                         // this.props); save to persist redux
  127.                         this
  128.                             .props
  129.                             .saveUserToken(data.token);
  130.                         this
  131.                             .props
  132.                             .openDashboard();
  133.                     }
  134.                     // setInitialRoute('HomeScreen', this.props);
  135.                     // this.props.navigation.navigate('HomeScreen');
  136.                 } catch (error) {
  137.                     ToastAndroid.show(error.message, 0);
  138.                     this.setState({isReady: false});
  139.                 }
  140.             }
  141.         }
  142.     };
  143.  
  144.     render() {
  145.         return (
  146.             <View style={styles.container}>
  147.                 <Text style={styles.headerText}>Login QInsurrance</Text>
  148.                 <View style={styles.direktion}>
  149.                     <FontAwesome name='user' size={40} style={styles.user}/>
  150.                     <QTextInput
  151.                         textStyle={styles.EditText}
  152.                         label={''}
  153.                         underlineColorAndroid='transparent'
  154.                         placeholderText="Username"
  155.                         blurOnSubmit={false}
  156.                         returnKeyType={"next"}
  157.                         onSubmitEditing={() => {
  158.                         this.focusNext('password')
  159.                     }}
  160.                         ref={input => {
  161.                         this.inputFile['username'] = input;
  162.                     }}
  163.                         onTextChanged={this.handleUserName}/>
  164.                 </View>
  165.  
  166.                 <View style={styles.direktion}>
  167.                     <FontAwesome name='lock' size={40} style={styles.lock}/>
  168.                     <QTextInput
  169.                         textStyle={styles.EditText}
  170.                         underlineColorAndroid="transparent"
  171.                         placeholderText="Password"
  172.                         blurOnSubmit={true}
  173.                         label={''}
  174.                         secureText={this.state.hidePassword}
  175.                         returnKeyType={"done"}
  176.                         onSubmitEditing={() => {
  177.                         this.SomethisReing()
  178.                     }}
  179.                         ref={input => {
  180.                         this.inputFile['password'] = input;
  181.                     }}
  182.                         onTextChanged={this.handlePassword}/>
  183.  
  184.                     <TouchableOpacity
  185.                         activeOpacity={0.8}
  186.                         style={styles.visibilityBtn}
  187.                         onPress={this.managePasswordVisibility}>
  188.                             {
  189.                                 this.state.hidePassword
  190.                             ?<Entypo name="eye-with-line" size={30}/>
  191.                             :<Entypo name="eye" size={30}/>
  192.                             }    
  193.                     </TouchableOpacity>
  194.                    
  195.                 </View>
  196.                
  197.               {this.state.isReady
  198.                         ? <ActivityIndicator color="blue" size="small" animating/>
  199.                         :
  200.                 <QButtonText
  201.                     btnStyle={styles.EditButton}
  202.                     btnText={styles.buttonText}
  203.                     content={'LOGIN'}  
  204.                     onPress={this.SomethisReing}
  205.                     />
  206.               }
  207.  
  208.                 <View
  209.                     style={[
  210.                     styles.direktion, {
  211.                     justifyContent: 'center',
  212.                     marginTop: 5
  213.                     }
  214.                 ]}>
  215.                     <QButtonText
  216.                         activeOpacity={0.7}
  217.                         btnStyle={styles.buttonView}
  218.                         onPress={() => this.props.goTo("Register")}
  219.                         content={'No Account yet? Register Here'}/>
  220.                 </View>
  221.             </View>
  222.         );
  223.     };
  224. };
  225.  
  226. const styles = StyleSheet.create({
  227.     direktion: {
  228.         flexDirection: 'row',
  229.  
  230.     },
  231.     Icons: {
  232.         height: 50,
  233.         width: 50,
  234.         marginLeft: 10,
  235.         color: 'black'
  236.     },
  237.     container: {
  238.         flex: 1,
  239.         backgroundColor: '#364854',
  240.         justifyContent: 'center',
  241.         paddingLeft: 60,
  242.         paddingRight: 60
  243.     },
  244.     headerText: {
  245.         fontWeight: 'bold',
  246.         alignSelf: 'center',
  247.         fontSize: 24,
  248.         color: '#fff',
  249.         paddingBottom: 10,
  250.         marginBottom: 20,
  251.         borderBottomWidth: 1,
  252.         borderBottomColor: '#199283',
  253.         alignSelf: 'center'
  254.     },
  255.     EditText: {
  256.         padding: 10,
  257.         marginTop: 5,
  258.         marginLeft: 5,
  259.         width: 260,
  260.         alignSelf: 'stretch',
  261.         height: 40,
  262.         marginBottom: 30,
  263.         color: '#fff',
  264.         borderBottomWidth: 1,
  265.         borderBottomColor: '#fff'
  266.     },
  267.     EditButton: {
  268.         alignItems: 'center',
  269.         backgroundColor: '#59cbbd',
  270.         paddingTop: 5,
  271.         paddingBottom: 5,
  272.         borderRadius: 20,
  273.         borderWidth: 1
  274.     },
  275.     visibilityBtn:
  276.     {
  277.       position: 'absolute',
  278.       right: 1,
  279.       top: 10,
  280.       height: 40,
  281.       width: 40,
  282.       padding: 5,
  283.      
  284.     },
  285.     user: {
  286.         paddingBottom: 10,
  287.         color: 'black',
  288.         alignSelf: 'center'
  289.     },
  290.     lock: {
  291.         paddingBottom: 10,
  292.         color: 'black',
  293.         alignSelf: 'center'
  294.     },
  295.     buttonText: {
  296.         color: '#000',
  297.         fontWeight: 'bold',
  298.         fontSize: 20
  299.     },
  300.     buttonView: {
  301.         flexDirection: 'row',
  302.         backgroundColor: 'transparent',
  303.         borderRadius: null,
  304.         padding: null
  305.     }
  306. })
  307.  
  308. const mapStateToProps = (state, props) => {
  309.     return {user: state.user};
  310. };
  311.  
  312. const mapDispatchToProps = (dispatch, props) => {
  313.     return {
  314.         goTo: (page, params) => dispatch(NavigationActions.navigate({routeName: page, params: params})),
  315.         saveUserToken: (token) => dispatch(saveToken(token)),
  316.         openDashboard: () => dispatch(NavigationActions.reset({
  317.             index: 0,
  318.             actions: [NavigationActions.navigate({routeName: 'Home'})]
  319.         }))
  320.     };
  321. };
  322.  
  323. export default connect(mapStateToProps, mapDispatchToProps)(LoginScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement