/
auth_nav.js
61 lines (48 loc) · 1.42 KB
/
auth_nav.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react';
import PropTypes from 'prop-types';
import { withApollo, graphql } from 'react-apollo';
import ApolloClient from 'apollo-client';
import { withCookies, Cookies } from 'react-cookie';
import { Link } from 'react-router-dom';
import { NavItem } from 'reactstrap';
import CURRENT_USER from '../graphql/current_user.graphql';
const logoutHelper = (cookies) => {
cookies.remove('x-token');
cookies.remove('x-refresh-token');
window.localStorage.setItem('token', null);
window.localStorage.setItem('refreshToken', null);
};
class AuthNav extends React.Component {
constructor(props) {
super(props);
}
logout = async () => {
const { cookies } = this.props;
await logoutHelper(cookies);
this.props.client.resetStore();
//this.props.data.refetch();
};
render() {
const { currentUser } = this.props.data;
if (currentUser) {
return (
<NavItem onClick={this.logout}><Link to="/">Logout</Link></NavItem>
);
} else {
return (
<NavItem><Link to="/login">Login</Link></NavItem>
);
}
}
}
AuthNav.propTypes = {
client: PropTypes.instanceOf(ApolloClient),
cookies: PropTypes.instanceOf(Cookies),
data: PropTypes.object,
};
export default withApollo(withCookies(graphql(CURRENT_USER, {
options: { fetchPolicy: 'network-only' },
//props: ({ data: { loading, currentUser } }) => ({
// loading, currentUser,
//}),
})(AuthNav)));