]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - cmd/web/js/otp.jsx
JS clean.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / web / js / otp.jsx
1 import Api from './api.js';
2 import App from './app.js';
3 import classNames from 'classnames';
4 import React from 'react';
5
6 class OtpQrCode extends React.Component {
7 render = () => {
8 return (
9 <div>
10 <img src={this.props.img} />
11 <p>{this.props.secret}</p>
12 </div>
13 );
14 }
15 }
16
17 class OtpEnrollForm extends React.Component {
18 constructor(props) {
19 super(props);
20 this.state = {'hideMsg': true, 'msg': '', 'msgOk': false, 'pass': ''};
21 }
22
23 handleSubmit = (e) => {
24 Api.Call('OTP_VALIDATE', {'pass': this.state.pass}, function(err, status, data) {
25 if (err) {
26 console.error(err, data);
27 this.displayMessage(App.errorCodeToMessage(err.code), false);
28 return;
29 }
30
31 this.displayMessage('OK', true);
32 this.props.onSuccess(data.token);
33
34 }.bind(this));
35
36 e.preventDefault();
37 }
38
39 handlePassChange = (event) => {
40 this.setState({'pass': event.target.value});
41 }
42
43 hideMessage = () => {
44 this.setState({'hideMsg': true});
45 }
46
47 displayMessage = (msg, ok) => {
48 this.setState({'msg': msg, 'msgOk': ok, 'hideMsg': false});
49 }
50
51 render = () => {
52 var cName = classNames('form-message', {'hidden': this.state.hideMsg, 'message-ok': this.state.msgOk});
53 var qrCode = null;
54
55 if (this.props.img) {
56 qrCode =
57 <div className="row justify-content-center">
58 <p>Please setup 2FA (Google Authenticator, Authy)</p>
59 <OtpQrCode img={this.props.img} secret={this.props.secret} />
60 </div>;
61 }
62 return (
63 <div className="row otp-enroll">
64 <div className="offset-1 col-10 box offset-md-4 col-md-4 text-center">
65 {qrCode}
66 <div className="row justify-content-center">
67 <form role="form" onSubmit={this.handleSubmit}>
68 <label className="w-100 text-left"><strong>Code</strong></label>
69 <input className="form-control" type="pass" onChange={this.handlePassChange} />
70 <input className="form-control submit" type="submit" value="Validate" />
71 <div className={cName}>{this.state.msg}</div>
72 </form>
73 </div>
74 </div>
75 </div>
76 );
77 }
78 }
79
80 export default OtpEnrollForm;