This repository was archived by the owner on Oct 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHome.tsx
72 lines (62 loc) · 1.84 KB
/
Home.tsx
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
62
63
64
65
66
67
68
69
70
71
72
import * as React from 'react';
import { RouteComponentProps, Link } from 'react-router-dom';
import { Button, Typography } from '@material-ui/core';
import { accountsClient, accountsRest } from './accounts';
interface State {
user: any;
twoFactorSecret: any;
}
class Home extends React.Component<RouteComponentProps<{}>, State> {
state = {
user: null as any,
twoFactorSecret: null as any,
};
async componentDidMount() {
// refresh the session to get a new accessToken if expired
const tokens = await accountsClient.refreshSession();
if (!tokens) {
this.props.history.push('/login');
return;
}
const res = await fetch('http://localhost:4000/user', {
headers: {
'accounts-access-token': tokens ? tokens.accessToken : '',
},
});
const user = await res.json();
this.setState({ user: user.user });
}
onResendEmail = async () => {
const { user } = this.state;
await accountsRest.sendVerificationEmail(user.emails[0].address);
};
onLogout = async () => {
await accountsClient.logout();
this.props.history.push('/login');
};
render() {
const { user } = this.state;
if (!user) {
return null;
}
return (
<div>
<Typography gutterBottom>You are logged in</Typography>
<Typography gutterBottom>Email: {user.emails[0].address}</Typography>
<Typography gutterBottom>
You email is {user.emails[0].verified ? 'verified' : 'unverified'}
</Typography>
{!user.emails[0].verified && (
<Button onClick={this.onResendEmail}>
Resend verification email
</Button>
)}
<Link to="two-factor">Set up 2fa</Link>
<Button variant="raised" color="primary" onClick={this.onLogout}>
Logout
</Button>
</div>
);
}
}
export default Home;