Skip to content

Instantly share code, notes, and snippets.

@Vidacs
Created January 13, 2018 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vidacs/aed687f599654c35e1a6df318510b1b9 to your computer and use it in GitHub Desktop.
Save Vidacs/aed687f599654c35e1a6df318510b1b9 to your computer and use it in GitHub Desktop.
// import React from 'react';
// import { StyleSheet, Text, View , AsyncStorage} from 'react-native';
// import GuestNavigation from './app/navigations/guestNavigation';
// import LoggedNavigation from './app/navigations/loggedNavigation';
// const USER_ACCESS = 'user_access';
// export default class App extends React.Component {
// constructor(props){
// super(props)
// this.state = {
// Logged: false,
// user_access: this.props.user_access,
// }
// this.storeSesion.bind(this)
// this.getSesion.bind(this)
// }
// async storeSesion(user_access){
// try{
// await AsyncStorage.setItem(USER_ACCESS, user_access);
// this.getSesion()
// } catch(error){
// console.log("Algo fue mal ")
// }
// }
// async getSesion(){
// try{
// let sesion = await AsyncStorage.getItem(USER_ACCESS);
// console.log("User chipocludo is: "+ sesion);
// if(sesion!==null){
// this.setState ({
// Logged: true,
// })
// } else {
// this.setState({
// Logged: false,
// })
// }
// } catch(error){
// console.log("Algo fue mal ")
// }
// }
// componentWillMount(){
// this.storeSesion()
// }
// render() {
// // if(this.state.isLogged)
// // {
// // return (<LoggedNavigation/>)
// // } else{
// // return(<GuestNavigation/>)
// // }
// console.log(USER_ACCESS);
// const {Logged} = this.state;
// console.log(Logged);
// if(!Logged){
// return(<GuestNavigation/>)
// }else{
// return(<LoggedNavigation/>)
// }
// }
// }
// const styles = StyleSheet.create({
// container: {
// flex: 1,
// backgroundColor: '#fff',
// alignItems: 'center',
// justifyContent: 'center',
// },
// });
import React from 'react';
import { StyleSheet, Text, View , AsyncStorage} from 'react-native';
import GuestNavigation from './app/navigations/guestNavigation';
import LoggedNavigation from './app/navigations/loggedNavigation';
const USER_ACCESS = 'user_access';
export default class App extends React.Component {
constructor(props){
super(props)
this.state = {
Logged: false,
user_access: this.props.user_access,
}
this.getSesion.bind(this)
}
async getSesion(){
try{
let sesion = await AsyncStorage.getItem(USER_ACCESS);
console.log("User is: "+ sesion);
if(sesion!==null){
this.setState ({
Logged: true,
})
} else {
this.setState({
Logged: false,
})
}
} catch(error){
console.log("Algo fue mal ")
}
}
componentWillMount(){
this.getSesion();
}
render() {
console.log(USER_ACCESS);
const {Logged} = this.state;
if(!Logged){
return(<GuestNavigation/>)
}else{
return(<LoggedNavigation/>)
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
// Register(){
// const {navigate} = this.props.navigation;
// return navigate('Home');
// }
{
"expo": {
"name": "test1",
"description": "This project is really great.",
"slug": "test1",
"privacy": "public",
"sdkVersion": "24.0.0",
"platforms": ["ios", "android"],
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"ios": {
"supportsTablet": true
}
}
}
import React from 'react';
import {StackNavigator} from 'react-navigation';
import LoginScreen from '../screens/Login';
const navigationOptions = {
navigationsOptions: ({ navigation}) => ({
headerTitleStyle: { textAlign: 'center', color: 'white'},
headerStyle: { background: '#FF9933', borderWidth:1, borderBottomColor: 'white' },
}),
};
export default StackNavigator({
Login: {
screen: LoginScreen
}
})
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button,
TextInput,
TouchableOpacity,
AsyncStorage,
KeyboardAvoidingView
} from 'react-native';
// import Expo from 'expo';
import {StackNavigator} from 'react-navigation';
const USER_ACCESS = 'user_access';
export default class Home extends React.Component{
constructor(props){
super(props);
this.state = {
user_access: this.props.user_access,
}
this.removeSesion.bind(this)
this.logout.bind(this)
}
static navigationOptions = {
drawerLabel: 'Home',
headerTitle: 'Bienvenido',
}
logout = () => {
this.removeSesion();
console.log('Por que no vas al home?')
}
async removeSesion(user_access){
try{
await AsyncStorage.removeItem(USER_ACCESS);
console.log(USER_ACCESS)
this.props.navigation.navigate('Login')
} catch(error){
console.log("Algo fue mal ")
}
}
Bluetooth = () =>{
this.goBle();
}
goBle(){
this.props.navigation.navigate('Bluetooth');
}
render(){
return(
<View style={styles.container}>
<Text style = { styles.text } >Área de Perfil {this.state.user_access}</Text>
<TouchableOpacity
style = {styles.btn}
onPress = {this.logout}>
<Text style={styles.btnC}>Log out2</Text>
</TouchableOpacity>
<TouchableOpacity
style = {styles.btn}
onPress = {this.Bluetooth}>
<Text style={styles.btnC}>Bluetooth</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex:1,
alignItems: 'stretch',
justifyContent: 'center',
backgroundColor: '#2896d3',
paddingLeft: 40,
paddingRight: 40,
},
text: {
color: '#fff',
},
btn: {
alignSelf: 'stretch',
backgroundColor: '#01c853',
padding: 20,
alignItems: 'center',
},
btnC: {
color: '#fff',
},
});
import React from 'react';
import {StackNavigator, DrawerNavigator} from 'react-navigation';
import HomeScreen from '../screens/Home';
const navigationOptions = {
navigationsOptions: ({ navigation}) => ({
headerTitleStyle: { textAlign: 'center', color: 'white'},
headerStyle: { background: '#FF9933', borderWidth:1, borderBottomColor: 'white' },
}),
};
const HomeInicio = StackNavigator({
Home:{
screen: HomeScreen
},
})
export default DrawerNavigator({
Home: {
screen: HomeInicio
}
})
import React from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import LoginUser from '../components/LoginForm';
export default class Login extends React.Component {
static navigationOptions = {
headerTitle: 'Iniciar sesión',
}
render() {
return (
<LoginUser navigation={this.props.navigation}/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button,
TextInput,
TouchableOpacity,
AsyncStorage,
KeyboardAvoidingView,
ScrollView,
AppRegistry
} from 'react-native';
// import {StackNavigator} from 'react-navigation';
const USER_ACCESS = 'user_access';
export default class LoginForm extends React.Component{
constructor(props){
super(props);
this.state = {
username: '',
password: '',
email: '',
error: '',
// isLogged: AsyncStorage.getItem('isLogged') == 'true' ? true: false,
// logged: AsyncStorage.getItem('USER_ACCESS')
}
// this.login.bind(this)
// this.storeSesion.bind(this)
// this.getSesion.bind(this)
// this.removeSesion.bind(this)
}
static navigationOptions = {
headerTitle: 'Iniciar sesión',
}
// componentDidMount(){
// AsyncStorage.getItem('USER_ACCESS').then((res) =>{
// const USER_ACCESS=JSON.parse(res);
// console.log(USER_ACCESS);
// if(USER_ACCESS){
// this.setState({
// isLogged: true,
// logged: USER_ACCESS
// });
// }
// }).done();
// }
// componentWillMount(){
// this.getSesion();
// }
async storeSesion(user_access){
try{
await AsyncStorage.setItem(USER_ACCESS, user_access);
this.getSesion()
} catch(error){
console.log("Algo fue mal ")
}
}
async getSesion(){
try{
let sesion = await AsyncStorage.getItem(USER_ACCESS);
console.log("User is: "+ sesion);
if(!sesion){
console.log("hola")
}else{
// this.props.navigation.navigate('Home')
console.log("Bien")}
} catch(error){
console.log("Algo fue mal ")
}
}
async removeSesion(){
try{
await AsyncStorage.removeItem(USER_ACCESS);
this.getSesion();
} catch(error){
console.log("Algo fue mal ")
}
}
render(){
return(
<View style={styles.container}>
<Text style = { styles.header } >-Login-</Text>
<TextInput
style = {styles.textInput} placeholder='Email'
onChangeText = { (email) => this.setState({email}) }
underlineColorAndroid = 'transparent'
/>
<TextInput
style = {styles.textInput} placeholder='Password'
onChangeText = { (password) => this.setState({password}) }
underlineColorAndroid = 'transparent'
secureTextEntry={true}
/>
<TouchableOpacity
style = {styles.btn}
onPress = {this.login.bind(this)}>
<Text style={ styles.btnC}>Login</Text>
</TouchableOpacity>
</View>
);
}
login = () =>{
try{
console.log("inicio");
fetch('http://162.243.136.245/apiapp/public/index.php/api/v1/autenticando'
, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: this.state.username,
password: this.state.password,
email: this.state.email,
})
})
.then((responseJson) => {
console.log(responseJson);
console.log(JSON.parse(responseJson._bodyInit));
var dataJson = JSON.parse(responseJson._bodyInit);
let user_access = dataJson.data.email;
if( responseJson.status >= 200 && responseJson.status < 300){
if(this.state.email == dataJson.data.email){
this.storeSesion(user_access)
console.log(user_access)
const {navigate} = this.props.navigation;
return navigate('Home');
// this.props.navigation.navigate('Home')
} else{
alert("Usuario no registrado");
}
}
else{
alert("Usuario no registrado");
}
})
}
// .done();
catch(error){
// this.removeSesion();
this.setState({error: error});
console.log("error"+error)
}
}
}
const styles = StyleSheet.create({
wrapper: {
flex: 1,
},
container: {
flex:1,
alignItems: 'stretch',
justifyContent: 'center',
backgroundColor: '#2896d3',
paddingLeft: 40,
paddingRight: 40,
},
header: {
fontSize: 24,
marginBottom: 60,
color: '#fff',
fontWeight: 'bold',
},
textInput: {
alignSelf: 'stretch',
padding: 16,
marginBottom: 20,
backgroundColor: '#fff',
},
btn: {
alignSelf: 'stretch',
backgroundColor: '#01c853',
padding: 20,
alignItems: 'center',
},
btnC: {
color: '#fff',
}
});
// import React, { Component } from 'react';
// import {
// Platform,
// StyleSheet,
// Text,
// View,
// Button,
// TextInput,
// TouchableOpacity,
// AsyncStorage,
// KeyboardAvoidingView,
// ScrollView,
// AppRegistry
// } from 'react-native';
// import {StackNavigator} from 'react-navigation';
// const USER_ACCESS = 'user_access';
// export default class LoginForm extends React.Component{
// constructor(props){
// super(props);
// this.state = {
// username: '',
// password: '',
// email: '',
// error: '',
// // isLogged: AsyncStorage.getItem('isLogged') == 'true' ? true: false,
// // logged: AsyncStorage.getItem('USER_ACCESS')
// }
// // this.login.bind(this)
// // this.storeSesion.bind(this)
// // this.getSesion.bind(this)
// // this.removeSesion.bind(this)
// }
// static navigationOptions = {
// headerTitle: 'Iniciar sesión',
// }
// // componentDidMount(){
// // AsyncStorage.getItem('USER_ACCESS').then((res) =>{
// // const USER_ACCESS=JSON.parse(res);
// // console.log(USER_ACCESS);
// // if(USER_ACCESS){
// // this.setState({
// // isLogged: true,
// // logged: USER_ACCESS
// // });
// // }
// // }).done();
// // }
// // componentWillMount(){
// // this.getSesion();
// // }
// async storeSesion(user_access){
// try{
// await AsyncStorage.setItem(USER_ACCESS, user_access);
// this.getSesion()
// } catch(error){
// console.log("Algo fue mal ")
// }
// }
// async getSesion(){
// try{
// let sesion = await AsyncStorage.getItem(USER_ACCESS);
// console.log("User is: "+ sesion);
// if(!sesion){
// console.log("hola")
// }else{
// this.props.navigation.navigate('Home')
// console.log("Bien")}
// } catch(error){
// console.log("Algo fue mal ")
// }
// }
// async removeSesion(){
// try{
// await AsyncStorage.removeItem(USER_ACCESS);
// this.getSesion();
// } catch(error){
// console.log("Algo fue mal ")
// }
// }
// render(){
// return(
// <View style={styles.container}>
// <Text style = { styles.header } >-Login-</Text>
// <TextInput
// style = {styles.textInput} placeholder='Email'
// onChangeText = { (email) => this.setState({email}) }
// underlineColorAndroid = 'transparent'
// />
// <TextInput
// style = {styles.textInput} placeholder='Password'
// onChangeText = { (password) => this.setState({password}) }
// underlineColorAndroid = 'transparent'
// secureTextEntry={true}
// />
// <TouchableOpacity
// style = {styles.btn}
// onPress = {this.login.bind(this)}>
// <Text style={ styles.btnC}>Login</Text>
// </TouchableOpacity>
// </View>
// );
// }
// login = () =>{
// try{
// console.log("inicio");
// fetch('http://162.243.136.245/apiapp/public/index.php/api/v1/autenticando'
// , {
// method: 'POST',
// headers: {
// 'Accept': 'application/json',
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// name: this.state.username,
// password: this.state.password,
// email: this.state.email,
// })
// })
// .then((responseJson) => {
// console.log(responseJson);
// console.log(JSON.parse(responseJson._bodyInit));
// var dataJson = JSON.parse(responseJson._bodyInit);
// let user_access = dataJson.data.email;
// if( responseJson.status >= 200 && responseJson.status < 300){
// if(this.state.email == dataJson.data.email){
// this.storeSesion(user_access)
// console.log(user_access)
// this.props.navigation.navigate('Home')
// } else{
// alert("Usuario no registrado");
// }
// }
// else{
// alert("Usuario no registrado");
// }
// })
// }
// // .done();
// catch(error){
// // this.removeSesion();
// this.setState({error: error});
// console.log("error"+error)
// }
// }
// }
// const styles = StyleSheet.create({
// wrapper: {
// flex: 1,
// },
// container: {
// flex:1,
// alignItems: 'stretch',
// justifyContent: 'center',
// backgroundColor: '#2896d3',
// paddingLeft: 40,
// paddingRight: 40,
// },
// header: {
// fontSize: 24,
// marginBottom: 60,
// color: '#fff',
// fontWeight: 'bold',
// },
// textInput: {
// alignSelf: 'stretch',
// padding: 16,
// marginBottom: 20,
// backgroundColor: '#fff',
// },
// btn: {
// alignSelf: 'stretch',
// backgroundColor: '#01c853',
// padding: 20,
// alignItems: 'center',
// },
// btnC: {
// color: '#fff',
// }
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment