从我的应用程序注销时想删除fb身份验证令牌

时间:2018-08-24 06:21:55

标签: reactjs local-storage facebook-access-token facebook-authentication

使用fb登录Web应用程序时,我保存FB访问令牌,并将令牌保存在localStorage中 我退出Facebook时要从localStorage中删除FB令牌

import React, { Component } from 'react';
import FacebookLogin from 'react-facebook-login';
import {Button} from 'reactstrap' 
import axios from 'axios';

class App extends Component {

    constructor() {
        super();      if(localStorage.getItem('isAuthenticated')&&localStorage.getItem(fbtoken)){
            this.state = { isAuthenticated: true, user:localStorage.getItem('name')};
        }
        else{
            this.state = { isAuthenticated: false, user:''}
        }
    }
    logout = () => {
      localStorage.setItem('token',null);
      localStorage.setItem('name',null);
      localStorage.setItem('isAuthenticated',false)
      this.setState({isAuthenticated: false})
    };
    
    facebookResponse = (e) => {
        console.log(e);
        var data={
            fb_id:e.email,
            fb_token:e.accessToken
        } 
        if(e.accessToken){
            axios.post('http://localhost:4007/loginn',data).then((res)=>{
                if(res.status===200){
                    localStorage.setItem('token',res.data.token);
                    localStorage.setItem('name',res.data.name);
                    localStorage.setItem('isAuthenticated',true);
                    localStorage.setItem('fbtoken',e.accessToken);
                    //this.forceUpdate();   
                    this.setState({isAuthenticated: true})
                }
                else{
                    alert('wrong email');
                }
            })
            .catch((err)=>{
                console.log('hahah');
            })
        }
    };

    onFailure = (error) => {
      alert(error);
    }
    render() {
        let content = !!this.state.isAuthenticated?
            (
                <div>
                    <p>Authenticated</p>
                    <div>
                        {localStorage.getItem('name')}
                    </div>
                    <div>
                        <button onClick={this.logout} className="button">
                            Log out
                        </button>
                    </div>
                </div>
            ) :
            (
                <div>
                    <FacebookLogin
                        appId="*************"
                        autoLoad={false}
                        fields="name,email,picture"
                        callback={this.facebookResponse} />
                </div>
            );

        return (
            <div className="App">
                {content}
            </div>
        );
    }
}

export default App;

当我从浏览器中退出FB时,我希望从localStorage中删除FB令牌 我需要再次登录才能访问我的Web应用程序 谢谢你的帮助

/ ** 它要求我写更多的文字,所以,我正在写更多的文字,请不要让我写更多的文字,我没有更多的问题,请不要这样做。 ** /

0 个答案:

没有答案