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 pathTwoFactor.tsx
79 lines (70 loc) · 1.93 KB
/
TwoFactor.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
73
74
75
76
77
78
79
import * as React from 'react';
import { RouteComponentProps } from 'react-router-dom';
import {
Button,
Typography,
FormControl,
InputLabel,
Input,
} from '@material-ui/core';
import * as QRCode from 'qrcode.react';
import { accountsRest } from './accounts';
interface State {
secret: any;
oneTimeCode: string;
}
class TwoFactor extends React.Component<RouteComponentProps<{}>, State> {
state = {
secret: null as any,
oneTimeCode: '',
};
async componentDidMount() {
this.onGetTwoFactorSecret();
}
onGetTwoFactorSecret = async () => {
const { secret } = await accountsRest.getTwoFactorSecret();
this.setState({ secret });
};
onChangeOneTimeCode = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ oneTimeCode: target.value });
};
onSetTwoFactor = async () => {
try {
await accountsRest.twoFactorSet(
this.state.secret,
this.state.oneTimeCode
);
} catch (err) {
console.log(err);
alert(err.message);
}
};
render() {
const { secret, oneTimeCode } = this.state;
if (!secret) {
return null;
}
return (
<div>
<Typography gutterBottom>Two-factor authentication</Typography>
<Typography gutterBottom>Backup code:</Typography>
<Typography gutterBottom>{secret.base32}</Typography>
<Typography gutterBottom>
Use Google Authenticator for example
</Typography>
<QRCode value={secret.otpauth_url} />
<Typography gutterBottom>Confirm with one-time code:</Typography>
<FormControl margin="normal">
<InputLabel htmlFor="one-time-code">One time code</InputLabel>
<Input
id="one-time-code"
value={oneTimeCode}
onChange={this.onChangeOneTimeCode}
/>
</FormControl>
<Button onClick={this.onSetTwoFactor}>Submit</Button>
</div>
);
}
}
export default TwoFactor;