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

mendersoftware / mender-server / 10423

11 Nov 2025 04:53PM UTC coverage: 74.435% (-0.1%) from 74.562%
10423

push

gitlab-ci

web-flow
Merge pull request #1071 from mendersoftware/dependabot/npm_and_yarn/frontend/main/development-dependencies-92732187be

3868 of 5393 branches covered (71.72%)

Branch coverage included in aggregate %.

5 of 5 new or added lines in 2 files covered. (100.0%)

176 existing lines in 95 files now uncovered.

64605 of 86597 relevant lines covered (74.6%)

7.74 hits per line

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

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

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

2✔
19
import Loader from '@northern.tech/common-ui/Loader';
2✔
20
import Form from '@northern.tech/common-ui/forms/Form';
2✔
21
import TextInput from '@northern.tech/common-ui/forms/TextInput';
2✔
22
import { twoFAStates } from '@northern.tech/store/constants';
2✔
23

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

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

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

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

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

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

2✔
120
export default AuthSetup;
2✔
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