使用访问令牌错误反应本机注销

时间:2021-02-12 08:01:54

标签: react-native

这是一个使用 API 登录和注销的简单应用。我在登录时检索了访问令牌。我将访问令牌导航到侧边栏并尝试使用注销功能注销

SideBar.js

import React from 'react';
import { Text, Alert } from 'react-native';
import { Drawer,Container, Content, Header, Right, Button } from 'native-base';

export default class SideBar extends React.Component {
constructor(props) {

    super(props)

    this.state = {

        token: this.props.usertoken

    }

}

UserLogoutFunction = () =>{

    const { token }  = this.state;
    
    
    fetch('https://api.idepoz.com/ncl/api/logout', {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Authorization': `Bearer ${token}`,
    },
    body: JSON.stringify({
    
        
    })
    
    }).then((response) => response.json())
        .then((responseJson) => {
            console.log("token");
            if(responseJson)
            {
               
                this.props.navigation.navigate('Login');
                Alert.alert("Succesfully Logged Out");
            }
            else{
    
                Alert.alert(responseJson);
            }
    
        }).catch((error) => {
            console.error(error);
        });

}
render() {
    return (
        <Container>
            <Header>
            </Header>
            <Content>
            
            <Button transparent onPress={ this.UserLogoutFunction } >
                <Text style={{fontSize: 24}}>Log Out</Text>       
            </Button>  
            </Content>
        </Container>
    );
    }
    }

它返回网络请求失败。

以下函数在登录时返回访问令牌

UserLoginFunction = () =>{

    const { UserName }  = this.state ;
    const { UserPassword }  = this.state ;
    
    
    fetch('https://api.idepoz.com/ncl/api/login', {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
    
        username: UserName,
    
        password: UserPassword
    
    })
    
    }).then((response) => response.json())
        .then((responseJson) => {
            
            if(responseJson)
            {
                
                this.props.navigation.navigate('QrScan',{usertoken:responseJson.token});
            }
            else{
    
                Alert.alert(responseJson);
            }
    
        }).catch((error) => {
            console.error(error);
        });

        }

请告诉“UserLogoutFunction”有什么问题

enter image description here

0 个答案:

没有答案