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

mendersoftware / gui / 901187442

pending completion
901187442

Pull #3795

gitlab-ci

mzedel
feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

Ticket: None
Changelog: None
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3795: feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

4389 of 6365 branches covered (68.96%)

5 of 5 new or added lines in 1 file covered. (100.0%)

1729 existing lines in 165 files now uncovered.

8274 of 10019 relevant lines covered (82.58%)

144.86 hits per line

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

67.9
/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);
9✔
29
  const currentUser = useSelector(getCurrentUser);
9✔
30
  const has2FA = useSelector(getHas2FA);
9✔
31
  const qrImage = useSelector(state => state.users.qrCode);
9✔
32
  const [qrExpanded, setQrExpanded] = useState(false);
9✔
33
  const [is2FAEnabled, setIs2FAEnabled] = useState(has2FA);
9✔
34
  const [showEmailVerification, setShowEmailVerification] = useState(false);
9✔
35
  const dispatch = useDispatch();
9✔
36

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

44
  useEffect(() => {
9✔
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]);
57

58
  useEffect(() => {
9✔
59
    if (has2FA) {
2!
UNCOV
60
      setIs2FAEnabled(has2FA);
×
61
    }
62
  }, [has2FA]);
63

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

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

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

122
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