• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1081664682

22 Nov 2023 02:11PM UTC coverage: 82.798% (-17.2%) from 99.964%
1081664682

Pull #4214

gitlab-ci

tranchitella
fix: Fixed the infinite page redirects when the back button is pressed

Remove the location and navigate from the useLocationParams.setValue callback
dependencies as they change the set function that is presented in other
useEffect dependencies. This happens when the back button is clicked, which
leads to the location changing infinitely.

Changelog: Title
Ticket: MEN-6847
Ticket: MEN-6796

Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
Pull Request #4214: fix: Fixed the infinite page redirects when the back button is pressed

4319 of 6292 branches covered (0.0%)

8332 of 10063 relevant lines covered (82.8%)

191.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.92
/src/js/components/settings/user-management/twofactorauth-steps/authsetup.js
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useEffect, useRef, useState } from 'react';
15

16
import { CheckCircle as CheckCircleIcon } from '@mui/icons-material';
17
import { Button } from '@mui/material';
18

19
import { twoFAStates } from '../../../../constants/userConstants';
20
import Form from '../../../common/forms/form';
21
import TextInput from '../../../common/forms/textinput';
22
import Loader from '../../../common/loader';
23

24
export const AuthSetup = ({ currentUser, handle2FAState, has2FA, onClose, qrImage, verify2FA }) => {
6✔
25
  const current2FA = useRef(has2FA);
10✔
26
  const [validated2fa, setValidated2fa] = useState(false);
10✔
27
  const [validating2fa, setValidating2fa] = useState(false);
10✔
28

29
  useEffect(() => {
10✔
30
    current2FA.current = has2FA;
3✔
31
  }, [has2FA]);
32

33
  useEffect(() => {
10✔
34
    const onUnload = e => {
4✔
35
      if (!e || (validated2fa && current2FA.current) || !qrImage) {
×
36
        return;
×
37
      }
38
      e.returnValue = '2fa setup incomplete';
×
39
      return e.returnValue;
×
40
    };
41

42
    window.addEventListener('beforeunload', onUnload);
4✔
43
    return () => {
4✔
44
      if (!current2FA.current && qrImage) {
4✔
45
        handle2FAState(twoFAStates.disabled);
1✔
46
      }
47
      window.removeEventListener('beforeunload', onUnload);
4✔
48
    };
49
  }, [handle2FAState, qrImage, validated2fa]);
50

51
  const validate2faSetup = formData => {
10✔
52
    setValidating2fa(true);
1✔
53
    formData.email = currentUser.email;
1✔
54
    return verify2FA(formData)
1✔
55
      .then(() => setValidated2fa(true))
1✔
56
      .catch(() => setValidated2fa(false))
×
57
      .finally(() => setValidating2fa(false));
1✔
58
  };
59

60
  return (
10✔
61
    <div className="margin-top">
62
      Setup:
63
      <div className="flexbox margin-top">
64
        <ol className="spaced-list margin-right-large" style={{ paddingInlineStart: 20 }}>
65
          <li className="margin-top-none">
66
            To use Two Factor Authentication, first download a third party authentication app such as{' '}
67
            <a href="https://authy.com/download/" target="_blank" rel="noopener noreferrer">
68
              Authy
69
            </a>{' '}
70
            or{' '}
71
            <a href="https://support.google.com/accounts/answer/1066447" target="_blank" rel="noopener noreferrer">
72
              Google Authenticator
73
            </a>
74
            .
75
          </li>
76
          <li>Scan the QR code on the right with the authenticator app you just downloaded on your device.</li>
77
          <li>
78
            <div>
79
              Type in the generated code in the input field below and click Verify.
80
              {validated2fa ? (
10✔
81
                <div className="flexbox space-between centered margin-top margin-right margin-bottom" style={{ justifyContent: 'flex-end' }}>
82
                  <CheckCircleIcon className="green" />
83
                  <h3 className="green margin-left-small" style={{ textTransform: 'uppercase' }}>
84
                    Verified
85
                  </h3>
86
                </div>
87
              ) : (
88
                <>
89
                  <Form showButtons={!validating2fa} buttonColor="primary" onSubmit={validate2faSetup} submitLabel="Verify">
90
                    <TextInput hint="Verification code" label="Verification code" id="token2fa" validations="isLength:6,isNumeric" required={true} />
91
                  </Form>
92
                  {validating2fa && (
10✔
93
                    <div className="flexbox" style={{ alignItems: 'flex-end', justifyContent: 'flex-end', height: 'min-content' }}>
94
                      <Loader show={true} />
95
                      <Button variant="contained" color="primary" disabled={true} style={{ marginLeft: 30 }}>
96
                        Verifying...
97
                      </Button>
98
                    </div>
99
                  )}
100
                </>
101
              )}
102
            </div>
103
          </li>
104
          <li>Then each time you log in, you will be asked for a verification code which you can retrieve from the authentication app on your device.</li>
105
        </ol>
106
        {!qrImage ? <Loader show={!qrImage} /> : <img src={`data:image/png;base64,${qrImage}`} style={{ maxHeight: '20vh' }} />}
10✔
107
      </div>
108
      <div className="flexbox" style={{ justifyContent: 'flex-end' }}>
109
        <Button onClick={() => handle2FAState(twoFAStates.disabled)} style={{ marginRight: 10 }}>
×
110
          Cancel
111
        </Button>
112
        <Button variant="contained" color="secondary" disabled={!validated2fa} onClick={onClose}>
113
          Save
114
        </Button>
115
      </div>
116
    </div>
117
  );
118
};
119

120
export default AuthSetup;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc