Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
117
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, Button, TextInput, TouchableOpacity } from 'react-native';
  3. import Home from './Home';
  4. export default class App extends Component{
  5.   state = {
  6.     username: '',
  7.     password: '',
  8.     auth_token: ''
  9.   }
  10.  
  11. Signup = async () => {
  12.     fetch('https://auth.clustername+.hasura-app.io/v1/signup', {
  13.       method: 'post',
  14.       headers: {
  15.         'Content-Type': 'application/json'
  16.       },
  17.       body: JSON.stringify({
  18.         "provider": "username",
  19.         "data": {
  20.         "username": this.state.username,
  21.         "password": this.state.password
  22.         }
  23.         })
  24.     }).then((response) => response.json())
  25.     .then((res) => {
  26.       if(typeof(res.message) != "undefined"){
  27.       Alert.alert("Error signing up", "Error: "+ res.message);
  28.  
  29. }
  30.       else{
  31.       this.setState({ auth_token: res.auth_token });
  32.       Alert.alert("Success", "You have succesfully signed up");
  33.       }
  34.     }).catch((error) => {
  35.     console.error(error);
  36.     });
  37.   }
  38.   Login = async () => {
  39.     fetch('https://auth.clustername.hasura-app.io/v1/login', {
  40.           method: 'post',
  41.           headers: {
  42.             'Content-Type': 'application/json'
  43.           },
  44.           body: JSON.stringify({
  45.             "provider": "username",
  46.             "data": {
  47.                 "username": this.state.username,
  48.                 "password": this.state.password
  49.             }
  50.           })
  51.         }).then((response) => response.json())
  52.         .then((res) => {
  53.       if(typeof(res.message) != "undefined"){
  54.        Alert.alert("Error", "Error: "+ res.message);
  55.       }
  56.       else{
  57.         this.setState({ auth_token: res.auth_token });
  58.         Alert.alert("Welcome", " You have succesfully logged in");
  59.         }
  60.      }).catch((error) => {
  61.          console.error(error);
  62.         });
  63.   }
  64.   render(){
  65.   //If auth token is not present
  66.    if(this.state.auth_token==''){
  67.      return(
  68.      <View>
  69.      <TextInput
  70.            placeholder="Enter User name"
  71.            onChangeText={ TextInputValue =>
  72.            this.setState({username : TextInputValue }) }
  73.            underlineColorAndroid='transparent'
  74.            style={
  75.            {
  76.                textAlign: 'center',
  77.                width: '90%',
  78.                marginBottom: 7,
  79.                height: 40,
  80.                borderRadius: 5 ,
  81.                fontSize: 20,
  82.            }
  83.          }
  84.          />
  85.      <TextInput
  86.            placeholder="Enter password"
  87.            onChangeText={ TextInputValue =>
  88.            this.setState({password: TextInputValue }) }
  89.            underlineColorAndroid='transparent'
  90.            secureTextEntry={true}
  91.            style={
  92.            {
  93.                textAlign: 'center',
  94.                width: '90%',
  95.                marginBottom: 7,
  96.                height: 40,
  97.                borderRadius: 5 ,
  98.                fontSize: 20,
  99.            }
  100.          }
  101.          />
  102.         <TouchableOpacity onPress={this.Signup.bind(this)}>
  103.         <View style={{height: 50, backgroundColor:
  104.         'purple',justifyContent: 'center',
  105.         alignItems: 'center',}}>
  106.           <Text style={{
  107.           fontSize: 20,
  108.           color: '#FFFFFF',
  109.           }}>
  110.           Signup</Text>
  111.         </View>
  112.         </TouchableOpacity>
  113.         <TouchableOpacity onPress={this.Login.bind(this)}>
  114.         <View style={{height: 50, backgroundColor:
  115.         'purple',justifyContent: 'center',
  116.         alignItems: 'center',}}>
  117.           <Text style={{
  118.           fontSize: 20,
  119.           color: '#FFFFFF',
  120.           }}>
  121.           Login </Text>
  122.         </View>
  123.         </TouchableOpacity>
  124.      </View>
  125.         );
  126.       }
  127.  
  128. /* Checking if the auth token is not empty directly sending the user to Home screen */
  129.       else{
  130.         return(
  131.           <Home />
  132.         );
  133.     }
  134.  
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement