autologin

This commit is contained in:
henrydays 2019-02-17 00:46:36 +00:00
parent 4fc96107f3
commit 0c2a2d5b5c
7 changed files with 225 additions and 311 deletions

View File

@ -299,106 +299,7 @@ class App extends Component {
</Swiper>
);
{
//se não existir vai para o ecrã de scan QR
return (
<QRCodeScanner
ref={(node) => { this.scanner = node }}
showMarker
onRead={this.onSuccess.bind(this)}
cameraStyle={{height: SCREEN_HEIGHT}}
customMarker={
<View style={styles.rectangleContainer}>
<Modal
isVisible={this.state.isModalVisible}
style={
{
backgroundColor:'#E8E8E8',
borderRadius:10,
marginTop:SCREEN_HEIGHT /this.state.push,
height:SCREEN_HEIGHT/2
}
}
animationInTiming={1000}
animationOutTiming={1000}
>
<View style={{ flex: 1}}>
<Text></Text>
<Text> Introduza a password</Text>
<RkTextInput
onFocus={this._print}
maxLength={10}
blurOnSubmit ={true}
secureTextEntry={true}
rkType='rounded'
onChangeText={(text) => this.setState({text})}
clearButtonMode='always'
value={this.state.text}
clearTextOnFocus={true}
onSubmitEditing={Keyboard.dismiss}
/>
<Button onPress={this._tryLogin} title="Login" color="#841584" />
{ this.props.failedAttempt &&
<Text> Password ou QR incorrecto</Text>
}
<Text></Text>
<Button onPress={this._activate} title="Scan again!" color="green" accessibilityLabel="Learn more about this purple button"/>
</View>
</Modal>
<View style={styles.logo}>
<Image style={UtilStyles.loginImage}
source={require('./assets/img/logo.png')}
/>
</View>
<View style={{flexDirection: "row"}}>
<View style={styles.leftAndRightOverlay}>
</View>
<View style={styles.rectangle}>
</View>
<View style={styles.leftAndRightOverlay}>
</View>
</View>
<View style={styles.bottomOverlay}>
<View style={{flex: 1, alignItems: 'center', alignContent: 'center'}}>
<RkText rkType='primary' style={styles.recover}>Recuperar pin de acesso</RkText>
<RkButton rkType='dark' style={styles.manual}>lols</RkButton>
</View>
</View>
</View>
}
/>
)
}
}
}
}

View File

@ -29,7 +29,7 @@ import * as Actions from '../store/actions'; //Import your actionss
import ImageOverlay from "react-native-image-overlay";
import {createStore} from 'redux';
import PTRView from 'react-native-pull-to-refresh';
import {Provider} from 'react-redux'
import { ScrollView } from 'react-native-gesture-handler';
const SCREEN_HEIGHT = Dimensions.get("window").height;
@ -51,7 +51,16 @@ const shadowOpt = {
}
class Home extends Component {
_refresh() {
return new Promise((resolve) => {
setTimeout(()=>{resolve()}, 2000)
});
}
_callApi(){
this.props.getUserInfo(this.props.token);
}
constructor(props) {
super(props);
@ -96,15 +105,12 @@ class Home extends Component {
//var navigate = this.props.navigation.navigate
}
_logout = () => {
// this.props.navigation.navigate('scan');
// this.props.logout();
this.props.logoutUser();
}
_update=()=>{
if(this.props.token!= undefined)
this.props.getUserInfo(this.props.token);
}
render() {
@ -123,6 +129,7 @@ class Home extends Component {
if (this.props.logged) {
return (
<PTRView onRefresh={this._update} >
<ScrollView style={{backgroundColor:'#eeeeee'}}>
<View>
@ -140,6 +147,7 @@ shadowColor: 'black',
shadowOpacity: 1.0
}
}>
<View style={styles.homeHeader}>
<View style={styles.userImageContainer}>
<Image style={styles.userImage} source={{uri: 'https://i.imgur.com/XXJ7LxV.jpg'}}/>
@ -147,6 +155,7 @@ shadowOpacity: 1.0
<Text style={styles.userText}>{this.props.user.Name}</Text>
<Text style={styles.userTextSub}>{this.props.user.Company}</Text>
</View>
</ImageBackground>
@ -185,18 +194,31 @@ shadowOpacity: 1.0
</View>
<View style={styles.userStats}>
<Text style={styles.userStatsTitle}>O meu saldo:</Text>
<View style={styles.userStatsBox}>
<View style={styles.userBox}>
<Icon name="ios-beer" style={styles.userStatsBoxIcon} size={50}/>
<Text style={styles.userBoxText}>0 Fino(s)</Text>
</View>
<View style={styles.userBox}>
<Icon name="ios-beer" style={styles.userStatsBoxIcon} size={50}/>
<Text style={styles.userBoxText}>0 Fino(s)</Text>
</View>
<View style={styles.userBox}>
<Icon name="ios-beer" style={styles.userStatsBoxIcon} size={50}/>
<Text style={styles.userBoxText}>0 Fino(s)</Text>
</View>
</View>
</View>
<Button onPress={this._logout} title="LOGOUT"/>
</View>
</ScrollView>
</PTRView>
);
}
else {
@ -214,7 +236,24 @@ shadowOpacity: 1.0
}
const styles = StyleSheet.create({
userBoxText:{
color:'white',
fontWeight:'bold'
},
userStatsBoxIcon:{
color:'white'
},
userStatsBox:{
flex:1,
flexDirection:'row'
},
userBox:{
alignItems:'center',
justifyContent:'center',
width:'33%',
},
userCurriculum:{
paddingTop:5,
color:'red',
@ -280,7 +319,14 @@ const styles = StyleSheet.create({
padding:10,
margin:9,
borderRadius:5,
marginTop:0,
marginTop:0,
},
userStatsTitle:{
color:'white',
fontSize:17,
fontWeight:'bold',
padding:1
},
userImageContainer:{
alignSelf: 'center',

View File

@ -1,5 +1,5 @@
import React, {Component} from 'react'
import {Card, Icon} from 'react-native-elements'
import {Card, Divider} from 'react-native-elements'
import {
Image,
ImageBackground,
@ -10,7 +10,10 @@ import {
StyleSheet,
Text,
View,
TouchableOpacity
TouchableOpacity,
Dimensions,
TextInput,
Button
} from 'react-native'
import PropTypes from 'prop-types';
@ -25,6 +28,9 @@ import Email from '../components/Email';
import Separator from '../components/Separator';
import Tel from '../components/Telephone';
const SCREEN_HEIGHT = Dimensions.get("window").height;
const SCREEN_WIDTH = Dimensions.get("window").width;
import Icon from "react-native-vector-icons/Ionicons"
class Profile extends Component {
@ -38,78 +44,73 @@ class Profile extends Component {
tokenData: '',
loggedIn: false,
onHold: true,
user: {}
user: {},
cenas:{Name:'as'},
text:'',
};
}
onPressTel = number => {
Linking.openURL(`tel://${number}`).catch(err =>
console.log('Error:', err))
};
onPressSms = number => {
Linking.openURL(`sms:${number}`).catch(err =>
console.log('Error:', err))
};
onPressEmail = email => {
Linking.openURL(`mailto:${email}`).catch(err =>
console.log('Error:', err)
)
};
renderHeader = () => {
return (
<View style={styles.header}>
<View style={styles.headerContent}>
<View style={styles.avatarSquare}>
<Image style={styles.avatar}
source={{uri: `${this.props.user.Avatar}`}} resizeMode='contain'/>
</View>
<Text style={styles.name}> {this.props.user.Name} {this.props.user.LastName}</Text>
<Text style={styles.userInfo}> {this.props.user.City} </Text>
</View>
</View>
)
};
renderTel = () => {
return (
<Tel
name={this.props.user.Name}
number={this.props.user.Mobile}
onPressSms={this.onPressSms}
onPressTel={this.onPressTel}
/>
)
};
_logout = () => {
renderEmail = () => {
return (
<Email
key={`${this.props.user}`}
name={this.props.user.Name}
email={this.props.user.Email}
onPressEmail={this.onPressEmail}
/>
)
};
// this.props.navigation.navigate('scan');
// this.props.logout();
this.props.logoutUser();
}
render() {
return (
<ScrollView style={styles.scroll}>
<ScrollView>
<View style={styles.container}>
<Card containerStyle={styles.cardContainer}>
{this.renderHeader()}
{this.renderTel()}
{Separator()}
{this.renderEmail()}
</Card>
</View>
<View style={styles.userBio}>
<View style={styles.userBioRow}>
<Icon name="ios-laptop" style={styles.userBioLogo} size={25}/>
<TextInput
onFocus={this._print}
maxLength={50}
blurOnSubmit ={true}
onChangeText={(text) => this.setState({text})}
value={this.props.user.Job}
/>
</View>
<Divider style={{ backgroundColor: 'black' }} />
<View style={styles.userBioRow}>
<Icon name="ios-mail" style={styles.userBioLogo} size={25}/>
<Text style={styles.userBioText}>{this.props.user.Email}</Text>
</View>
<Divider style={{ backgroundColor: 'black' }} />
<View style={styles.userBioRow}>
<Icon name="ios-phone-portrait" style={styles.userBioLogo} size={25}/>
<Text style={styles.userBioText}>{this.props.user.Mobile}</Text>
</View>
<Divider style={{ backgroundColor: 'black' }} />
<View style={styles.userBioRow}>
<Icon name="ios-map" style={styles.userBioLogo} size={25}/>
<Text style={styles.userBioText}>{this.props.user.Address}, {this.props.user.City}</Text>
</View>
<Divider style={{ backgroundColor: 'black' }} />
<View style={styles.userBioRow}>
<Icon name="ios-person" style={styles.userBioLogo} size={25}/>
<TouchableOpacity><Text style={styles.userCurriculum}>O meu Curriculo</Text></TouchableOpacity>
</View>
<Button onPress={this._logout} title="LOGOUT"/>
</View>
</View>
</ScrollView>
)
}
@ -117,77 +118,21 @@ class Profile extends Component {
const styles = StyleSheet.create({
scroll: {
backgroundColor: '#FFF',
},
container: {
flex: 1,
},
cardContainer: {
backgroundColor: '#FFF',
borderWidth: 0,
flex: 1,
margin: 0,
padding: 0,
},
emailContainer: {
color: '#000',
paddingTop: 30,
alignItems: 'center',
},
telContainer: {
color: '#000',
paddingTop: 30,
alignItems: 'center',
},
contact_container: {
backgroundColor: '#FFF',
margin: 20,
//height: 150,
},
header: {
backgroundColor: '#CC6666',
},
headerContent: {
padding: 30,
alignItems: 'center',
},
avatar: {
/* borderWidth: 4,*/
transform: [{rotateZ: '20deg'}],
/*borderColor: "#000",
marginBottom: 50,*/
flex: 1,
alignItems: 'center',
container:{
},
avatarSquare: {
width: 130,
height: 130,
transform: [{rotateZ: '-20deg'}],
borderWidth: 4,
borderColor: "#000",
marginBottom: 50,
backgroundColor: '#fff',
userBioRow:{
flex:1,
flexDirection:'row',
padding:10,
},
userBioText:{
},
userInfo: {
fontSize: 16,
color: "#778899",
fontWeight: '600',
},
data_content: {
backgroundColor: '#777',
userBioLogo:{
marginLeft: SCREEN_WIDTH*0.05,
width: SCREEN_WIDTH*0.15,
},

View File

@ -90,55 +90,8 @@ const deleteToken = async () => {
const renewToken=(refresh)=>{
var details = {
'grant_type': 'refresh_token',
'refresh_token':refresh
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
console.log(refresh);
fetch('http://enei2019.uingress.com/internal/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
}).catch(err=>{
}).then(parsed=>{
// console.log(a);
var obj={
access_token:parsed.access_token,
expirationDateToken:Math.round(new Date().getTime()/1000) + parsed.expires_in,
refreshToken:parsed.refresh_token,
valid:true
};
})
return obj;
}
@ -350,27 +303,90 @@ export function checkUser(){
//se expirar
if(Math.round(new Date().getTime()/1000) >= a.expirationDateToken){
refresh=a.refreshToken
// a.valid=false;
//chamar funçao para renovar
console.log("expirou")
renewToken(a.refreshToken).then(b=>{
// a.valid=true;
deleteToken();
saveToken(b);
console.log("asdasdasdasd")
dispatch({type: CHECK_USER, token:b, logged:true, onHold:false});
var details = {
'grant_type': 'refresh_token',
'refresh_token':refresh
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
fetch('http://enei2019.uingress.com/internal/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
}).then(res=>res.json()).then(parsed=>{
console.log(parsed);
if(parsed.error=='invalid_grant'){
console.log(formBody);
dispatch({type: CHECK_USER, token:'', logged:false, onHold:false});
}else{
var obj={
access_token:parsed.access_token,
expirationDateToken:Math.round(new Date().getTime()/1000) + parsed.expires_in,
refreshToken:parsed.refresh_token,
valid:true
};
// deleteToken();
saveToken(obj).then(a=>{
console.log("saved" )
console.log(obj)
dispatch({type: CHECK_USER, token:obj, logged:true, onHold:false});
})
}
}).catch(a=>{
console.log("Putasss")
dispatch({type: CHECK_USER, token:'', logged:false, onHold:false});
})
}else{
console.log("Tempo restante token: "+ Math.round((a.expirationDateToken-Math.round(new Date().getTime()/1000) )/60) +" Minutos");
//fazer validação da data e renovar o token
dispatch({type: CHECK_USER, token:a, logged:true, onHold:false});
}
console.log("Tempo restante token: "+ Math.round((a.expirationDateToken-Math.round(new Date().getTime()/1000) )/60) +" Minutos");
//fazer validação da data e renovar o token
dispatch({type: CHECK_USER, token:a, logged:true, onHold:false});
}

View File

@ -32,7 +32,7 @@ const apiReducer = (state = apiState, action) => {
case USER_INFO:
state=Object.assign({},state, { user: action.user, token: action.token , loggedIn:action.loggedIn, onHold:action.onHold});
state=Object.assign({},state, { user: action.user , loggedIn:action.loggedIn, onHold:action.onHold});
return state;

5
App/package-lock.json generated
View File

@ -7876,6 +7876,11 @@
"prop-types": "^15.5.8"
}
},
"react-native-pull-to-refresh": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/react-native-pull-to-refresh/-/react-native-pull-to-refresh-2.1.3.tgz",
"integrity": "sha512-u/E4295psx/9RKqtz70elLdmiPoLK7PL5r6sQ7EMmM02FIBRp1GZibcWjauwgfKll9b6+QqCBez5mQOyfMF+SQ=="
},
"react-native-qrcode-scanner": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/react-native-qrcode-scanner/-/react-native-qrcode-scanner-1.1.2.tgz",

View File

@ -27,6 +27,7 @@
"react-native-material-shadows": "0.0.2",
"react-native-modal": "^7.0.2",
"react-native-progress": "^3.5.0",
"react-native-pull-to-refresh": "^2.1.3",
"react-native-qrcode-scanner": "^1.1.2",
"react-native-shadow": "^1.2.2",
"react-native-swiper": "^1.5.14",