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

mendersoftware / gui / 963002358

pending completion
963002358

Pull #3870

gitlab-ci

mzedel
chore: cleaned up left over onboarding tooltips & aligned with updated design

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3870: MEN-5413

4348 of 6319 branches covered (68.81%)

95 of 122 new or added lines in 24 files covered. (77.87%)

1734 existing lines in 160 files now uncovered.

8174 of 9951 relevant lines covered (82.14%)

178.12 hits per line

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

74.07
/src/js/components/settings/user-management/twofactorauthsetup.js
1
// Copyright 2019 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, { useCallback, useEffect, useState } from 'react';
15
import { useDispatch, useSelector } from 'react-redux';
16

17
import { Collapse, Switch } from '@mui/material';
18

19
import { setSnackbar } from '../../../actions/appActions';
20
import { disableUser2fa, enableUser2fa, get2FAQRCode, verify2FA, verifyEmailComplete, verifyEmailStart } from '../../../actions/userActions';
21
import { twoFAStates } from '../../../constants/userConstants';
22
import { getCurrentUser, getHas2FA } from '../../../selectors';
23
import InfoText from '../../common/infotext';
24
import AuthSetup from './twofactorauth-steps/authsetup';
25
import EmailVerification from './twofactorauth-steps/emailverification';
26

27
export const TwoFactorAuthSetup = () => {
6✔
28
  const activationCode = useSelector(state => state.users.activationCode);
22✔
29
  const currentUser = useSelector(getCurrentUser);
14✔
30
  const has2FA = useSelector(getHas2FA);
14✔
31
  const qrImage = useSelector(state => state.users.qrCode);
22✔
32
  const [qrExpanded, setQrExpanded] = useState(false);
14✔
33
  const [is2FAEnabled, setIs2FAEnabled] = useState(has2FA);
14✔
34
  const [showEmailVerification, setShowEmailVerification] = useState(false);
14✔
35
  const dispatch = useDispatch();
14✔
36

37
  useEffect(() => {
14✔
38
    if ((currentUser.verified || currentUser.email?.endsWith('@example.com')) && is2FAEnabled && !has2FA) {
4✔
39
      setShowEmailVerification(false);
1✔
40
      setQrExpanded(true);
1✔
41
    }
42
  }, [currentUser.email, currentUser.verified, is2FAEnabled, has2FA]);
43

44
  useEffect(() => {
14✔
45
    if (activationCode) {
2!
UNCOV
46
      setIs2FAEnabled(true);
×
UNCOV
47
      dispatch(verifyEmailComplete(activationCode))
×
48
        .catch(() => {
UNCOV
49
          setShowEmailVerification(true);
×
UNCOV
50
          setQrExpanded(false);
×
51
        })
52
        // we have to explicitly call this, to not send the returned promise as user to activate 2fa for
UNCOV
53
        .then(() => dispatch(enableUser2fa()))
×
UNCOV
54
        .then(() => dispatch(get2FAQRCode()));
×
55
    }
56
  }, [activationCode, dispatch]);
57

58
  useEffect(() => {
14✔
59
    if (has2FA) {
3✔
60
      setIs2FAEnabled(has2FA);
1✔
61
    }
62
  }, [has2FA]);
63

64
  const handle2FAState = useCallback(
14✔
65
    state => {
66
      setIs2FAEnabled(state !== twoFAStates.disabled);
2✔
67
      setQrExpanded(state === twoFAStates.unverified);
2✔
68
      let request;
69
      if (state === twoFAStates.disabled) {
2!
UNCOV
70
        request = dispatch(disableUser2fa());
×
71
      } else if (state === twoFAStates.enabled && has2FA) {
2✔
72
        request = Promise.resolve(setQrExpanded(false));
1✔
73
      } else {
74
        request = dispatch(enableUser2fa());
1✔
75
      }
76
      request.then(() => {
2✔
77
        if (state === twoFAStates.unverified) {
2✔
78
          dispatch(get2FAQRCode());
1✔
79
        } else if (state === twoFAStates.enabled) {
1!
80
          setSnackbar('Two Factor authentication set up successfully.');
1✔
81
        }
82
      });
83
    },
84
    [dispatch, has2FA]
85
  );
86

87
  const onToggle2FAClick = useCallback(() => {
14✔
88
    if (!(currentUser.verified || currentUser.email?.endsWith('@example.com'))) {
1!
UNCOV
89
      setShowEmailVerification(!showEmailVerification);
×
UNCOV
90
      setIs2FAEnabled(!showEmailVerification);
×
UNCOV
91
      return;
×
92
    }
93
    if (has2FA) {
1!
UNCOV
94
      handle2FAState(twoFAStates.disabled);
×
95
    } else {
96
      is2FAEnabled ? dispatch(disableUser2fa()) : handle2FAState(twoFAStates.unverified);
1!
97
      setQrExpanded(!is2FAEnabled);
1✔
98
      setIs2FAEnabled(!is2FAEnabled);
1✔
99
    }
100
  }, [currentUser.email, currentUser.verified, dispatch, handle2FAState, has2FA, is2FAEnabled, showEmailVerification]);
101

102
  return (
14✔
103
    <div className="margin-top">
104
      <div className="clickable flexbox space-between" onClick={onToggle2FAClick}>
105
        <p className="help-content">Enable Two Factor authentication</p>
106
        <Switch checked={is2FAEnabled} />
107
      </div>
108
      <InfoText style={{ width: '75%', margin: 0 }}>
109
        Two Factor Authentication adds a second layer of protection to your account by asking for an additional verification code each time you log in.
110
      </InfoText>
111
      {showEmailVerification && (
14!
112
        <EmailVerification
113
          activationCode={activationCode}
UNCOV
114
          verifyEmailComplete={data => dispatch(verifyEmailComplete(data))}
×
UNCOV
115
          verifyEmailStart={() => dispatch(verifyEmailStart())}
×
116
        />
117
      )}
118
      <Collapse in={qrExpanded} timeout="auto" unmountOnExit>
119
        <AuthSetup currentUser={currentUser} handle2FAState={handle2FAState} has2FA={has2FA} qrImage={qrImage} verify2FA={data => dispatch(verify2FA(data))} />
1✔
120
      </Collapse>
121
    </div>
122
  );
123
};
124

125
export default TwoFactorAuthSetup;
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