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

mendersoftware / gui / 1113439055

19 Dec 2023 09:01PM UTC coverage: 82.752% (-17.2%) from 99.964%
1113439055

Pull #4258

gitlab-ci

mender-test-bot
chore: Types update

Signed-off-by: Mender Test Bot <mender@northern.tech>
Pull Request #4258: chore: Types update

4326 of 6319 branches covered (0.0%)

8348 of 10088 relevant lines covered (82.75%)

189.39 hits per line

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

70.0
/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 = () => {
5✔
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!
46
      setIs2FAEnabled(true);
×
47
      dispatch(verifyEmailComplete(activationCode))
×
48
        .catch(() => {
49
          setShowEmailVerification(true);
×
50
          setQrExpanded(false);
×
51
        })
52
        // we have to explicitly call this, to not send the returned promise as user to activate 2fa for
53
        .then(() => dispatch(enableUser2fa()))
×
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);
1✔
67
      setQrExpanded(state === twoFAStates.unverified);
1✔
68
      let request;
69
      if (state === twoFAStates.disabled) {
1!
70
        request = dispatch(disableUser2fa());
×
71
      } else {
72
        request = dispatch(enableUser2fa());
1✔
73
      }
74
      request.then(() => {
1✔
75
        if (state === twoFAStates.unverified) {
1!
76
          dispatch(get2FAQRCode());
1✔
77
        } else if (state === twoFAStates.enabled) {
×
78
          setSnackbar('Two Factor authentication set up successfully.');
×
79
        }
80
      });
81
    },
82
    [dispatch]
83
  );
84

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

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

130
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