Guest User

Untitled

a guest
Dec 17th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. export class App extends Component {
  2. constructor() {
  3. super();
  4. this.state = {
  5. token: '',
  6. loading: true
  7. }
  8.  
  9. this.newJWT = this.newJWT.bind(this);
  10. this.deleteJWT = deviceStorage.deleteJWT.bind(this);
  11. this.loadJWT = deviceStorage.loadJWT.bind(this);
  12. this.loadJWT();
  13. }
  14.  
  15. newJWT(token){
  16. this.setState({
  17. token: token
  18. });
  19. }
  20.  
  21. render() {
  22. if (this.state.loading) {
  23. return (
  24. <Loading size={'large'} />
  25. );
  26. } else if (!this.state.token) {
  27. return (
  28.  
  29. <AppTabNavigator screenProps={{newJWT: this.newJWT }} /> // I think this is the issue
  30.  
  31. );
  32. } else if (this.state.token) {
  33. return (
  34. <AuthLoadingScreen screenProps= {{token: this.token}} deleteJWT={this.deleteJWT} /> // Could be second issue
  35. );
  36. }
  37. }
  38. }
  39.  
  40. const AuthStackNavigator = createStackNavigator({
  41. LoginScreen: {
  42. screen: LoginScreen,
  43. navigationOptions: {
  44. header: null
  45. }
  46. },
  47.  
  48. })
  49.  
  50. const AppTabNavigator = createMaterialTopTabNavigator({
  51. Profile: {
  52. screen: props => <LoggedIn {...props.screenProps} />,
  53. navigationOptions: {
  54. tabBarLabel: 'Profile',
  55. tabBarIcon: ({tintColor}) => (
  56. <Icon name = "ios-contact" color = {tintColor} size = {24}/>
  57. )
  58. }
  59.  
  60. },
  61. Home: {
  62. screen: HomeScreen,
  63. navigationOptions: {
  64. tabBarLabel: 'Home',
  65. tabBarIcon: ({tintColor}) => (
  66. <Icon name = "ios-home" color = {tintColor} size = {24}/>
  67. )
  68. }
  69. },
  70. Second: {
  71. screen: SecondScreen,
  72. navigationOptions: {
  73. tabBarLabel: 'Second',
  74. tabBarIcon: ({tintColor}) => (
  75. <Icon name = "ios-home" color = {tintColor} size = {24}/>
  76. )
  77. }
  78. },
  79.  
  80. }, {
  81. tabBarPosition: 'bottom',
  82. tabBarOptions: {
  83. activeTintColor: 'orange',
  84. inactiveTintColor: 'grey',
  85. style: {
  86. backgroundColor: '#f2f2f2',
  87. },
  88. indicatorStyle: {
  89. height: 0
  90. },
  91. showIcon: true
  92. },
  93. });
  94.  
  95.  
  96.  
  97. const abc = createSwitchNavigator(
  98. {
  99. AuthLoading: AuthLoadingScreen,
  100. Auth: AuthStackNavigator,
  101. App: AppTabNavigator
  102. },
  103. {
  104. initialRouteName: 'AuthLoading',
  105. })
  106.  
  107. const styles = StyleSheet.create({
  108. container: {
  109. flex: 1,
  110. alignItems: 'center',
  111. justifyContent: 'center'
  112. }
  113. });
  114.  
  115. export default createAppContainer(abc);
  116.  
  117. export default class LoginScreen extends React.Component {
  118.  
  119. constructor(props) {
  120.  
  121. super(props)
  122.  
  123. this.state = {
  124. username: '',
  125. password: '',
  126. error: '',
  127. loading: false
  128. };
  129.  
  130. this.loginUser = this.loginUser.bind(this);
  131. this.onLoginFail = this.onLoginFail.bind(this);
  132.  
  133. }
  134.  
  135. CheckTextInputIsEmptyOrNot = () =>{
  136.  
  137. const { username } = this.state ;
  138. const { password } = this.state ;
  139.  
  140. if(username == '' || password == '') {
  141.  
  142. Alert.alert("Please Enter All the Values.");
  143. }
  144.  
  145. else {
  146.  
  147. // Do something here which you want to if all the Text Input is filled.
  148.  
  149. this.loginUser();
  150.  
  151. }
  152.  
  153. }
  154.  
  155. loginUser() {
  156. const { username, password, password_confirmation } = this.state;
  157.  
  158. this.setState({ error: '', loading: true });
  159.  
  160. // NOTE Post to HTTPS only in production
  161. axios.post("http://192.168.0.196:8000/api/login",{
  162. username: username,
  163. password: password
  164. })
  165. .then((response) => {
  166. console.log('response',response)
  167. deviceStorage.saveKey("id_token", response.data.token);
  168. console.log(response.data.token);
  169. this.props.newJWT(response.data.token); // This is not working
  170. //this.props.navigation.navigate('App'); // This is working but it is not what i wanted
  171. })
  172. .catch((error) => {
  173. const status = error.response.status
  174. if (status === 401) {
  175. this.setState({ error: 'username or password not recognised.' });
  176. }
  177. this.onLoginFail();
  178. //console.log(error);
  179. //this.onLoginFail();
  180. });
  181. }
  182.  
  183. onLoginFail() {
  184. this.setState({
  185. error: 'Login Failed',
  186. loading: false
  187. });
  188. }
  189.  
  190. render() {
  191. // other codes here
  192. }
Add Comment
Please, Sign In to add comment