feat: enhance login logic with dynamic captcha display and add API for fetching authentication settings

v3.2
wxg0103 2025-09-17 15:11:26 +08:00
parent b96b499bc7
commit 680502f366
3 changed files with 302 additions and 221 deletions

View File

@ -50,11 +50,19 @@ const getLoginAuthSetting: (loading?: Ref<boolean>) => Promise<Result<any>> = (l
return get(`login/auth/setting`, undefined, loading) return get(`login/auth/setting`, undefined, loading)
} }
/**
*
*/
const getLoginViewAuthSetting: (auth_type: string, loading?: Ref<boolean>) => Promise<Result<any>> = (auth_type, loading) => {
return get(`login/${prefix}/${auth_type}/detail`, undefined, loading)
}
export default { export default {
getAuthSetting, getAuthSetting,
postAuthSetting, postAuthSetting,
putAuthSetting, putAuthSetting,
putLoginSetting, putLoginSetting,
getLoginSetting, getLoginSetting,
getLoginAuthSetting getLoginAuthSetting,
getLoginViewAuthSetting
} }

View File

@ -66,6 +66,7 @@
size="large" size="large"
class="input-item" class="input-item"
v-model="loginForm.username" v-model="loginForm.username"
@blur="handleUsernameBlur(loginForm.username)"
:placeholder="$t('views.login.loginForm.username.placeholder')" :placeholder="$t('views.login.loginForm.username.placeholder')"
> >
</el-input> </el-input>
@ -84,7 +85,7 @@
</el-input> </el-input>
</el-form-item> </el-form-item>
</div> </div>
<div class="mb-24" v-if="loginMode !== 'LDAP'"> <div class="mb-24" v-if="loginMode !== 'LDAP'&& showCaptcha">
<el-form-item prop="captcha"> <el-form-item prop="captcha">
<div class="flex-between w-full"> <div class="flex-between w-full">
<el-input <el-input
@ -100,7 +101,7 @@
alt="" alt=""
height="38" height="38"
class="ml-8 cursor border border-r-6" class="ml-8 cursor border border-r-6"
@click="makeCode" @click="makeCode(loginForm.username)"
/> />
</div> </div>
</el-form-item> </el-form-item>
@ -212,6 +213,7 @@ const loginForm = ref<LoginRequest>({
captcha: '', captcha: '',
}) })
const max_attempts = ref<number>(1) // ref
const rules = ref<FormRules<LoginRequest>>({ const rules = ref<FormRules<LoginRequest>>({
username: [ username: [
{ {
@ -253,20 +255,30 @@ const loginHandle = () => {
params: {accessToken: chatUser.accessToken}, params: {accessToken: chatUser.accessToken},
query: route.query, query: route.query,
}) })
localStorage.removeItem('chat_' + loginForm.value.username)
}).catch(() => {
const username = loginForm.value.username
localStorage.setItem('chat_' + username, String(Number(localStorage.getItem('chat_' + username) || '0') + 1))
loading.value = false
loginForm.value.username = ''
loginForm.value.password = ''
const timestampKey = `${username}_chat_first_fail_timestamp`
if (!localStorage.getItem(timestampKey)) {
localStorage.setItem(timestampKey, Date.now().toString())
}
}) })
} }
}) })
} }
function makeCode(userrname?: string) { function makeCode(username?: string) {
loginApi.getCaptcha(userrname).then((res: any) => { loginApi.getCaptcha(username).then((res: any) => {
identifyCode.value = res.data.captcha identifyCode.value = res.data.captcha
}) })
} }
onBeforeMount(() => { onBeforeMount(() => {
locale.value = chatUser.getLanguage() locale.value = chatUser.getLanguage()
makeCode()
}) })
const modeList = ref<string[]>([]) const modeList = ref<string[]>([])
@ -365,7 +377,59 @@ function changeMode(val: string) {
loginFormRef.value?.clearValidate() loginFormRef.value?.clearValidate()
} }
const showCaptcha = computed<boolean>(() => {
// -1
if (max_attempts.value === -1) {
return false
}
// 0
if (max_attempts.value === 0) {
return true
}
// 0
const username = loginForm.value.username?.trim()
if (!username) {
return false //
}
const timestampKey = `${username}_chat_first_fail_timestamp`
const firstFailTimestamp = localStorage.getItem(timestampKey)
if (firstFailTimestamp) {
const expirationTime = 60 * 60 * 1000 // 10
if (Date.now() - parseInt(firstFailTimestamp) > expirationTime) {
//
localStorage.removeItem('chat_' + username)
localStorage.removeItem(timestampKey)
return false
}
} else {
//
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
if (failCount > 0) {
localStorage.removeItem('chat_' + username)
return false
}
}
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
console.log('failCount', failCount)
return failCount >= max_attempts.value
})
function handleUsernameBlur(username: string) {
if (showCaptcha.value) {
makeCode(username)
}
}
onBeforeMount(() => { onBeforeMount(() => {
if (chatUser.chat_profile?.max_attempts) {
max_attempts.value = chatUser.chat_profile.max_attempts
}
if (chatUser.chat_profile?.login_value) { if (chatUser.chat_profile?.login_value) {
modeList.value = chatUser.chat_profile.login_value modeList.value = chatUser.chat_profile.login_value
if (modeList.value.includes('LOCAL')) { if (modeList.value.includes('LOCAL')) {

View File

@ -287,6 +287,9 @@ function handleUsernameBlur(username: string) {
} }
onBeforeMount(() => { onBeforeMount(() => {
user.asyncGetProfile().then((res) => {
//
if (user.isPE() || user.isEE()) {
authApi.getLoginAuthSetting().then((res) => { authApi.getLoginAuthSetting().then((res) => {
if (Object.keys(res.data).length > 0) { if (Object.keys(res.data).length > 0) {
authSetting.value = res.data; authSetting.value = res.data;
@ -307,6 +310,12 @@ onBeforeMount(() => {
} }
} }
}) })
} else {
authSetting.value = {
max_attempts: 1,
default_value: 'password',
}
}
}) })
const modeList = ref<string[]>(['']) const modeList = ref<string[]>([''])
@ -342,7 +351,7 @@ function redirectAuth(authType: string, needMessage: boolean = true) {
if (authType === 'LDAP' || authType === '') { if (authType === 'LDAP' || authType === '') {
return return
} }
authApi.getAuthSetting(authType, loading).then((res: any) => { authApi.getLoginViewAuthSetting(authType, loading).then((res: any) => {
if (!res.data || !res.data.config) { if (!res.data || !res.data.config) {
return return
} }