After setup a simple token login based in a tutorial and the react docs the react-native shows Can't find variable: isLogged i've tried to pass the getToken to isLogged var but show this Can't find variable error.
sorry, as i'm trying to learn js and the react in general terms.
in this case if the token exists redirect the user to home otherwise redirect to login. so, can anyone please spare a hint about how to pass a variable as a function in this scenario?
here is the code:
import React, { Component } from 'react';
import { AsyncStorage } from 'react-native';
import { NativeRouter, Route, Link, Redirect, withRouter } from 'react-router-native'
import Login from './pages/Login';
const AUTH_TOKEN = 'auth_token';
const isLogged = getToken();
class App extends Component {
componentWillMount() {
this.getToken();
}
async getToken() {
try {
let authToken = await AsyncStorage.getItem(AUTH_TOKEN);
if (!authToken) {
console.log("Token not set");
} else {
this.verifyToken(authToken)
}
} catch (error) {
console.log("Something went wrong");
}
}
async verifyToken(token) {
let authToken = token
try {
let response = await fetch('https://lpfoot.herokuapp.com/api/verify?session%5Bauth_token%5D='+authToken);
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
//Verified token means user is logged in.
} else {
//Handle error
let error = res;
throw error;
}
} catch(error) {
console.log("error response: " + error);
}
}
render() {
if (isLogged) {
return this.props.history.push('./pages/Home');
} else {
return this.props.history.push('./pages/Login');
}
}
}
export default App;