Advertisement
Guest User

Authentication

a guest
Sep 22nd, 2019
1,752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { View, Text, Alert, AsyncStorage } from 'react-native';
  3. // import firebase from 'firebase';
  4. import { Header, Spinner, Button, Card, CardSection, Input } from '../components/common';
  5. // import LoginForm from '../components/LoginForm';
  6.  
  7. class Authentication extends Component {
  8.     // state = { loggedIn: null, username: '', password: '' };
  9.  
  10.     // componentWillMount() {
  11.     //     firebase.initializeApp({
  12.     //         apiKey: "AIzaSyDGU-VemLGDev4saZzhVTF7_BJ5ff9PWx8",
  13.     //         authDomain: "testapp-ffddb.firebaseapp.com",
  14.     //         databaseURL: "https://testapp-ffddb.firebaseio.com",
  15.     //         projectId: "testapp-ffddb",
  16.     //         storageBucket: "testapp-ffddb.appspot.com",
  17.     //         messagingSenderId: "890498565190",
  18.     //     });
  19.  
  20.     //     firebase.auth().onAuthStateChanged((user) => {
  21.     //         if (user) {
  22.     //             this.setState({ loggedIn: true });
  23.     //         } else {
  24.     //             this.setState({ loggedIn: false });
  25.     //         }
  26.     //     });
  27.     // }
  28.  
  29.     // renderContent() {
  30.     //     switch (this.state.loggedIn) {
  31.     //         case true:
  32.     //             this.props.navigation.navigate("albums")
  33.     //         case false:
  34.     //             return <LoginForm />;
  35.     //         default:
  36.     //             return <Spinner size="large" />;
  37.     //     }
  38.     // }
  39.  
  40.     // funcClickLogin(){
  41.  
  42.     // }
  43.  
  44.     state = { username: '', password: '', error: '', loading: false };
  45.  
  46.     onButtonPress() {
  47.         const { username, password } = this.state;
  48.         this.setState({ error: '', loading: true });
  49.         const {navigate} = this.props.navigation;
  50.         // firebase.auth().signInWithEmailAndPassword(email, password)
  51.         //     .then(this.onLoginSuccess.bind(this))
  52.         //     .catch(this.onLoginFail.bind(this));
  53.         //     // .catch(() => {
  54.         //     //     this.onLoginFail.bind(this)
  55.         //     //     firebase.auth().createUserWithEmailAndPassword(email, password)
  56.         //     //         .then(this.onLoginSuccess.bind(this))
  57.         //     //         .catch(this.onLoginFail.bind(this));
  58.         //     // });
  59.         var tempData = new FormData();
  60.         tempData.append('username', username);
  61.         tempData.append('password', password);
  62.  
  63.         fetch('https://authservices.herokuapp.com/api/user/login', {
  64.             method: 'POST',
  65.             headers: {
  66.                 'Accept':'application/json',
  67.                 'Content-Type': 'application/x-www-form-urlencoded'
  68.             },
  69.             // body: JSON.stringify({
  70.             //     "userName": username,
  71.             //     "userPassword": password,
  72.             //     "apnToken": userapncode.toString()
  73.             // })
  74.             body:tempData
  75.         }).then((response) => response.json()).then((responseData) => {
  76.             if(responseData.status == 200){
  77.                 this.setState({ error: '', loading: false });
  78.                 AsyncStorage.setItem('appId', responseData.data.id)
  79.                 this.props.navigation.navigate("albums");
  80.                 Alert.alert('udemy', responseData.message)
  81.             } else {
  82.                 this.setState({ error: '', loading: false });
  83.                 Alert.alert('udemy', responseData.message)
  84.             }
  85.         }).done();
  86.         // this.props.navigation.navigate("albums");
  87.     }
  88.  
  89.     onPressRegister(){
  90.         this.props.navigation.navigate("register");
  91.     }
  92.  
  93.     onLoginFail() {
  94.         this.setState({ error: 'Authentication Failed', loading: false });
  95.     }
  96.  
  97.     onLoginSuccess() {
  98.         this.setState({
  99.             username: '',
  100.             password: '',
  101.             loading: false,
  102.             error: ''
  103.         });
  104.     }
  105.  
  106.     renderButton(getData) {
  107.         if (this.state.loading) {
  108.             return <Spinner size="small" />;
  109.         }
  110.         if(getData == 'login'){
  111.             return (
  112.                 <Button onPress={this.onButtonPress.bind(this)}>
  113.                     Log In
  114.                 </Button>
  115.             );
  116.         }else{
  117.             return (
  118.                 <Button onPress={()=>this.props.navigation.navigate("register")}>
  119.                     Registrasi
  120.                 </Button>
  121.             );
  122.         }
  123.     }
  124.  
  125.     render() {
  126.         return (
  127.             <View>
  128.                 <Header headerText="Authentication" />
  129.                 {/* {this.renderContent()} */}
  130.                 <Card>
  131.                     <CardSection>
  132.                         <Input
  133.                             placeholder="username"
  134.                             label="Username"
  135.                             value={this.state.username}
  136.                             onChangeText={username => this.setState({ username })}
  137.                         />
  138.                     </CardSection>
  139.  
  140.                     <CardSection>
  141.                         <Input
  142.                             secureTextEntry
  143.                             placeholder="password"
  144.                             label="Password"
  145.                             value={this.state.password}
  146.                             onChangeText={password => this.setState({ password })}
  147.                         />
  148.                     </CardSection>
  149.  
  150.                     <Text style={styles.errorTextStyle}>
  151.                         {this.state.error}
  152.                     </Text>
  153.  
  154.                     <CardSection>
  155.                         {this.renderButton('login')}
  156.                     </CardSection>
  157.                     <CardSection>
  158.                         {this.renderButton('registrasi')}
  159.                     </CardSection>
  160.                 </Card>
  161.             </View>
  162.         );
  163.     }
  164. }
  165.  
  166. const styles = {
  167.     errorTextStyle: {
  168.         fontSize: 20,
  169.         alignSelf: 'center',
  170.         color: 'red'
  171.     }
  172. };
  173.  
  174. export default Authentication;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement