Skip to content

Commit f65456d

Browse files
chore: added attribute called temporary while creating otp from wizard (#165)
* chore: added attribute called temporary while creating otp from wizard
1 parent 150fc50 commit f65456d

2 files changed

Lines changed: 65 additions & 38 deletions

File tree

src/components-v1/v2-login-manager.jsx

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ export default function V2LoginManager({ configuration, disableInputs }) {
8787
const [otpVerifyError, setOtpVerifyError] = useState('');
8888
const [otpAttemptsRemaining, setOtpAttemptsRemaining] = useState(null);
8989

90-
const [snackbarNetworkError, setSnackbarNetworkError] = useState(false);
90+
const [snackbarNetworkError, setSnackbarNetworkError] = useState('');
91+
const [showSnackbarDetails, setShowSnackbarDetails] = useState(false);
9192
const [detailNetworkModal, setDetailNetworkModal] = useState(false);
9293

9394
const submitCredentials = useCallback(
@@ -151,7 +152,8 @@ export default function V2LoginManager({ configuration, disableInputs }) {
151152
setLoadingCredentials(false);
152153
break;
153154
default:
154-
setSnackbarNetworkError(true);
155+
setShowSnackbarDetails(true);
156+
setSnackbarNetworkError(t('cant_login', 'Cannot log in now'));
155157
setLoadingCredentials(false);
156158
}
157159
})
@@ -188,7 +190,7 @@ export default function V2LoginManager({ configuration, disableInputs }) {
188190
[setDetailNetworkModal]
189191
);
190192
const onCloseSnackbarCbk = useCallback(
191-
() => setSnackbarNetworkError(false),
193+
() => setSnackbarNetworkError(''),
192194
[setSnackbarNetworkError]
193195
);
194196

@@ -200,25 +202,40 @@ export default function V2LoginManager({ configuration, disableInputs }) {
200202
setProgress(formState.credentials);
201203
}, []);
202204

203-
const onOtpWizardProceed = useCallback((otpLabel) => {
204-
setLoadingOtpSetup(true);
205-
generateOtp(otpLabel)
206-
.then((data) => {
207-
if (data.secret) {
208-
const uri = `otpauth://totp/${encodeURIComponent(data.label)}?secret=${data.secret}&issuer=${encodeURIComponent(data.issuer)}&algorithm=${data.algorithm}&digits=${data.digits_length}&period=${data.period}`;
209-
setOtpUri(uri);
210-
setOtpGeneratedId(data.id);
211-
setStaticOtpCodes(data.static_otp_codes || []);
212-
setProgress(formState.otpSetup);
213-
} else {
214-
setSnackbarNetworkError(true);
215-
}
216-
})
217-
.catch(() => {
218-
setSnackbarNetworkError(true);
219-
})
220-
.finally(() => setLoadingOtpSetup(false));
221-
}, []);
205+
const onOtpWizardProceed = useCallback(
206+
(otpLabel) => {
207+
setLoadingOtpSetup(true);
208+
generateOtp(otpLabel)
209+
.then((data) => {
210+
if (data.secret) {
211+
const uri = `otpauth://totp/${encodeURIComponent(data.label)}?secret=${data.secret}&issuer=${encodeURIComponent(data.issuer)}&algorithm=${data.algorithm}&digits=${data.digits_length}&period=${data.period}`;
212+
setOtpUri(uri);
213+
setOtpGeneratedId(data.id);
214+
setStaticOtpCodes(data.static_otp_codes || []);
215+
setProgress(formState.otpSetup);
216+
} else {
217+
setShowSnackbarDetails(false);
218+
setSnackbarNetworkError(
219+
t(
220+
'otp_generation_failed',
221+
'Something went wrong, please try again with another unique name or wait a couple of minutes before try again'
222+
)
223+
);
224+
}
225+
})
226+
.catch(() => {
227+
setShowSnackbarDetails(false);
228+
setSnackbarNetworkError(
229+
t(
230+
'otp_generation_failed',
231+
'Something went wrong, please try again with another unique name or wait a couple of minutes before try again'
232+
)
233+
);
234+
})
235+
.finally(() => setLoadingOtpSetup(false));
236+
},
237+
[t]
238+
);
222239

223240
const onVerifyOtpSetupCode = useCallback(
224241
(code, isTrustedDevice) => {
@@ -244,10 +261,15 @@ export default function V2LoginManager({ configuration, disableInputs }) {
244261
setOtpVerifyError('invalid');
245262
}
246263
})
247-
.catch(() => setSnackbarNetworkError(true))
264+
.catch(() => {
265+
setShowSnackbarDetails(false);
266+
setSnackbarNetworkError(
267+
t('otp_verification_failed', 'Failed to verify OTP. Please try again')
268+
);
269+
})
248270
.finally(() => setLoadingOtpSetup(false));
249271
},
250-
[otpGeneratedId]
272+
[otpGeneratedId, t]
251273
);
252274

253275
const onBackFromSetup = useCallback(() => {
@@ -401,10 +423,12 @@ export default function V2LoginManager({ configuration, disableInputs }) {
401423
<ForgetPassword configuration={configuration} disableInputs={disableInputs} />
402424
)}
403425
<Snackbar
404-
open={snackbarNetworkError}
405-
label={t('cant_login', 'Can not do the login now')}
406-
actionLabel={t('details', 'Details')}
407-
onActionClick={onSnackbarActionCbk}
426+
open={!!snackbarNetworkError}
427+
label={snackbarNetworkError}
428+
{...(showSnackbarDetails && {
429+
actionLabel: t('details', 'Details'),
430+
onActionClick: onSnackbarActionCbk
431+
})}
408432
onClose={onCloseSnackbarCbk}
409433
autoHideTimeout={10000}
410434
type="error"

src/services/v2-service.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ export function submitOtp(id, code, trustDevice) {
4343
}
4444

4545
export function generateOtp(labelPrefix) {
46-
return fetch(`/zx/auth/v2/otp/generate?labelPrefix=${encodeURIComponent(labelPrefix)}`, {
47-
method: 'POST',
48-
headers: {
49-
'Content-Type': 'application/json'
50-
},
51-
body: JSON.stringify({
52-
labelPrefix,
53-
humanReadable: false
54-
})
55-
}).then((res) => res.json());
46+
return fetch(
47+
`/zx/auth/v2/otp/generate?labelPrefix=${encodeURIComponent(labelPrefix)}&temporary=true`,
48+
{
49+
method: 'POST',
50+
headers: {
51+
'Content-Type': 'application/json'
52+
},
53+
body: JSON.stringify({
54+
labelPrefix,
55+
humanReadable: false
56+
})
57+
}
58+
).then((res) => res.json());
5659
}

0 commit comments

Comments
 (0)