-
-
Save daviddsp/0a16412c6bb2fc6a6d81cf431e3b4178 to your computer and use it in GitHub Desktop.
Login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import styles from '../style'; | |
| import { Text, View, StyleSheet, Image,Button,TextInput,TouchableHighlight, Alert, AsyncStorage } from 'react-native'; | |
| import Icon from 'react-native-vector-icons/SimpleLineIcons'; | |
| const token = 'accessToken'; | |
| export default class Login extends Component { | |
| constructor(props) { | |
| super(); | |
| this.state = { | |
| email: '', | |
| password:'', | |
| } | |
| } | |
| async storeToken(accessToken){ | |
| try { | |
| await AsyncStorage.setItem(token, accessToken); | |
| this.getToken(); | |
| } catch(error){ | |
| console.log('Error StoreItem') | |
| } | |
| } | |
| async getToken(){ | |
| try { | |
| let Token = await AsyncStorage.getItem(token); | |
| console.log('Token getToken:' + Token); | |
| } catch(error){ | |
| console.log('Error getToken') | |
| } | |
| } | |
| async removeToken(){ | |
| try { | |
| let Token = await AsyncStorage.removeItem(token); | |
| this.getToken(); | |
| } catch(error){ | |
| console.log('Error remove') | |
| } | |
| } | |
| changeEmail(email){ | |
| this.setState({email}) | |
| } | |
| changePassword(password){ | |
| this.setState({password}) | |
| } | |
| _Login = () => { | |
| var url = 'http://api.acknowlogy.tl2.thoughtlab.info/api/v1/authenticate'; | |
| var data = { email:this.state.email,password:this.state.password}; | |
| fetch (url, { | |
| method: 'POST', | |
| body:JSON.stringify(data), | |
| headers:{ | |
| Accept: 'application/json', | |
| 'Content-Type': 'application/json' | |
| } | |
| }) | |
| .then(res => res.json(console.log(res))) | |
| .then(resData => { | |
| try { | |
| AsyncStorage.setItem('@MySuperStore:token', resData.accessToken); | |
| } catch (error) { | |
| console.log("Error saving data" + error); | |
| } | |
| //this.storeToken(resData.accessToken); | |
| console.log("res token:" + resData.accessToken); | |
| console.log(resData); | |
| }) | |
| .then(response => {this.props.navigation.push('dashboard') | |
| }) | |
| .catch(error => console.error('Error:', error)); | |
| } | |
| render() { | |
| return ( | |
| <View style={styles.container}> | |
| <Image | |
| source={require('../assets/background5.png')} | |
| style={styles.bgapp} | |
| /> | |
| <Image | |
| source={require('../assets/icon.png')} | |
| style={styles.logo} | |
| /> | |
| <Text style={styles.title}>A C K N O W L O G Y</Text> | |
| <View style={styles.inputContainer}> | |
| <TextInput | |
| placeholder="Username" | |
| placeholderTextColor= "grey" | |
| underlineColorAndroid='transparent' | |
| inlineImageLeft='icon' | |
| style={styles.input} | |
| value={this.state.email} | |
| onChangeText={(email) => this.changeEmail(email)} | |
| /><Icon name="people" size={20} color="grey"/> | |
| </View> | |
| <View style={styles.inputContainer}> | |
| <TextInput | |
| placeholder="Password" | |
| autoCorrect={false} | |
| secureTextEntry | |
| placeholderTextColor= "grey" | |
| underlineColorAndroid='transparent' | |
| style={styles.input} | |
| value={this.state.password} | |
| onChangeText={(password) => this.changePassword(password)} | |
| /> | |
| <Icon name="lock-open" size={20} color="grey"/> | |
| </View> | |
| <Text style ={{color:'white'}} | |
| onPress={() => {this.props.navigation.navigate('forgot')}}> | |
| Forgot Your Password?</Text> | |
| <View> | |
| <TouchableHighlight | |
| style ={styles.button} | |
| onPress={this._Login} | |
| underlayColor={'gray'} | |
| > | |
| <Text style ={{color:'white', | |
| fontSize: 19, | |
| }}>LOG IN</Text> | |
| </TouchableHighlight> | |
| </View> | |
| <View style ={{flexDirection: 'row' }}> | |
| <Text style ={{color:'white', flexDirection: 'row'}}> Dont have an Account?</Text> | |
| <TouchableHighlight onPress={() => {this.props.navigation.push('signup')}}> | |
| <Text style ={{textDecorationLine:"underline", color:'white' | |
| }}> Sign Up </Text> | |
| </TouchableHighlight> | |
| </View> | |
| </View> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment