fix: 解决 mfa 验证回车时触发页面刷新的问题 (#383)

pull/385/head
ssongliu 2023-03-23 20:34:33 +08:00 committed by GitHub
parent 4f4879759e
commit 01bb6b7c01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 36 deletions

View File

@ -9,7 +9,6 @@ type UserLoginInfo struct {
Name string `json:"name"`
Token string `json:"token"`
MfaStatus string `json:"mfaStatus"`
MfaSecret string `json:"mfaSecret"`
}
type MfaCredential struct {
@ -28,7 +27,6 @@ type Login struct {
type MFALogin struct {
Name string `json:"name"`
Password string `json:"password"`
Secret string `json:"secret"`
Code string `json:"code"`
AuthMethod string `json:"authMethod"`
}

View File

@ -86,9 +86,9 @@ func (u *AuthService) MFALogin(c *gin.Context, info dto.MFALogin) (*dto.UserLogi
}
pass, err := encrypt.StringDecrypt(passwrodSetting.Value)
if err != nil {
return nil, constant.ErrAuth
return nil, err
}
if info.Password != pass && nameSetting.Value != info.Name {
if info.Password != pass || nameSetting.Value != info.Name {
return nil, constant.ErrAuth
}

View File

@ -9,7 +9,6 @@ export namespace Login {
export interface MFALoginForm {
name: string;
password: string;
secret: string;
code: string;
authMethod: string;
}
@ -17,7 +16,6 @@ export namespace Login {
name: string;
token: string;
mfaStatus: string;
mfaSecret: string;
}
export interface InitUser {
name: string;

View File

@ -120,6 +120,8 @@ const message = {
warnning:
'Note: [Closing the security entrance] will make your panel login address directly exposed to the Internet, very dangerous, please exercise caution',
codeInput: 'Please enter the 6-digit verification code of the MFA validator',
mfaTitle: 'MFA Certification',
mfaCode: 'MFA verification code',
title: 'Linux Server Management Panel',
licenseHelper:
'Agree to FIT2CLOUD &laquo; <a href="https://www.fit2cloud.com/legal/licenses.html" target="_blank">Community Software License Agreement</a> &raquo;',

View File

@ -123,7 +123,8 @@ const message = {
solutionHelper: ' SSH 1./etc/init.d/bt default',
warnning: '使',
codeInput: ' MFA 6 ',
mfaTitle: 'MFA',
mfaTitle: 'MFA ',
mfaCode: 'MFA ',
title: 'Linux ',
licenseHelper:
' FIT2CLOUD &laquo; <a href="https://www.fit2cloud.com/legal/licenses.html" target="_blank"> </a> &raquo;',

View File

@ -1,5 +1,5 @@
<template>
<div>
<div v-loading="loading">
<div v-if="isFirst">
<div class="login-form">
<el-form ref="registerFormRef" :model="registerForm" size="default" :rules="registerRules">
@ -70,13 +70,13 @@
</div>
<div v-else-if="mfaShow">
<div class="login-form">
<el-form>
<el-form @submit.prevent="mfaLogin()">
<div class="login-title">{{ $t('commons.login.mfaTitle') }}</div>
<el-form-item class="no-border">
<el-input
size="default"
:placeholder="$t('commons.login.captchaHelper')"
v-model="mfaLoginForm.code"
:placeholder="$t('commons.login.mfaCode')"
v-model.trim="mfaLoginForm.code"
>
<template #prefix>
<el-icon class="el-input__icon">
@ -89,7 +89,15 @@
</span>
</el-form-item>
<el-form-item>
<el-button class="login-button" type="primary" size="default" round @click="mfaLogin()">
<el-button
@focus="mfaButtonFocused = true"
@blur="mfaButtonFocused = false"
class="login-button"
type="primary"
size="default"
round
@click="mfaLogin()"
>
{{ $t('commons.button.verify') }}
</el-button>
</el-form-item>
@ -233,6 +241,8 @@ const loginRules = reactive({
name: [{ required: true, message: i18n.global.t('commons.rule.username'), trigger: 'blur' }],
password: [{ required: true, message: i18n.global.t('commons.rule.password'), trigger: 'blur' }],
});
const mfaButtonFocused = ref();
const mfaLoginForm = reactive({
name: '',
password: '',
@ -266,41 +276,39 @@ const login = (formEl: FormInstance | undefined) => {
if (!formEl) return;
formEl.validate(async (valid) => {
if (!valid) return;
loading.value = true;
let requestLoginForm = {
name: loginForm.name,
password: loginForm.password,
captcha: loginForm.captcha,
captchaID: captcha.captchaID,
authMethod: '',
};
if (requestLoginForm.captcha == '') {
errCaptcha.value = true;
return;
}
if (loginForm.agreeLicense == false) {
errAgree.value = true;
return;
}
try {
let requestLoginForm = {
name: loginForm.name,
password: loginForm.password,
captcha: loginForm.captcha,
captchaID: captcha.captchaID,
authMethod: '',
};
if (requestLoginForm.captcha == '') {
errCaptcha.value = true;
return;
}
if (loginForm.agreeLicense == false) {
errAgree.value = true;
return;
}
loading.value = true;
const res = await loginApi(requestLoginForm);
if (res.code === 406) {
if (res.message === 'ErrCaptchaCode') {
errCaptcha.value = true;
errAuthInfo.value = false;
loginVerify();
}
if (res.message === 'ErrAuth') {
errCaptcha.value = false;
errAuthInfo.value = true;
loginVerify();
}
loginVerify();
return;
}
if (res.data.mfaStatus === 'enable') {
mfaShow.value = true;
errMfaInfo.value = false;
mfaLoginForm.secret = res.data.mfaSecret;
return;
}
globalStore.setLogStatus(true);
@ -366,12 +374,16 @@ onMounted(() => {
document.onkeydown = (e: any) => {
e = window.event || e;
if (e.keyCode === 13) {
if (loading.value) return;
if (isFirst.value && !registerButtonFocused.value) {
register(registerFormRef.value);
if (!mfaShow.value) {
if (isFirst.value && !registerButtonFocused.value) {
register(registerFormRef.value);
}
if (!isFirst.value && !loginButtonFocused.value) {
login(loginFormRef.value);
}
}
if (!isFirst.value && !loginButtonFocused.value) {
login(loginFormRef.value);
if (mfaShow.value && !mfaButtonFocused.value) {
mfaLogin();
}
}
};