335 lines
15 KiB
Vue
335 lines
15 KiB
Vue
<script setup lang="ts">
|
|
import { onMounted, reactive, ref } from 'vue'
|
|
import { useI18n } from 'vue-i18n'
|
|
import { useRoute, useRouter } from 'vue-router'
|
|
import http, { apiErrorMessage } from '../api/http'
|
|
import { useAuthStore } from '../stores/auth'
|
|
import { isNativeApp } from '../auth/session'
|
|
import { beginNativeSsoLogin, openSsoLogin } from '../services/nativeAppLinks'
|
|
import { createPasskey, isPasskeyCancellation, passkeysSupported } from '../services/passkeys'
|
|
import { formatDate } from '../i18n/format'
|
|
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const auth = useAuthStore()
|
|
const { t, te } = useI18n()
|
|
const loading = ref(true)
|
|
const actionLoading = ref(false)
|
|
const totp = reactive({ enabled: false, setupUri: '', recoveryCodes: [] as string[] })
|
|
const passkey = reactive({
|
|
enabled: false,
|
|
supported: passkeysSupported(),
|
|
items: [] as any[],
|
|
})
|
|
const sso = reactive({
|
|
enabled: false,
|
|
providerDisplayName: '',
|
|
isBound: false,
|
|
callbackUrl: '',
|
|
})
|
|
|
|
function translatedCode(prefix: string, value: string) {
|
|
const key = `${prefix}.${value}`
|
|
return te(key) ? t(key) : value
|
|
}
|
|
|
|
async function loadAccount() {
|
|
loading.value = true
|
|
try {
|
|
const { data } = await http.get('/auth/sso/account')
|
|
sso.enabled = Boolean(data.enabled)
|
|
sso.providerDisplayName = String(data.providerDisplayName || sso.providerDisplayName)
|
|
sso.isBound = Boolean(data.isBound)
|
|
sso.callbackUrl = String(data.callbackUrl || '')
|
|
const totpResult = await http.get('/auth/security/totp')
|
|
totp.enabled = Boolean(totpResult.data.enabled)
|
|
const passkeyResult = await http.get('/auth/passkeys')
|
|
passkey.enabled = Boolean(passkeyResult.data.enabled)
|
|
passkey.items = passkeyResult.data.items ?? []
|
|
} catch (error) {
|
|
ElMessage.error(apiErrorMessage(error))
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function addPasskey() {
|
|
try {
|
|
const password = await ElMessageBox.prompt(
|
|
t('auth.account.passkey.passwordPrompt'),
|
|
t('auth.account.passkey.addTitle'),
|
|
{ inputType: 'password', inputValidator: value => Boolean(value) || t('auth.account.passwordRequired') },
|
|
)
|
|
const name = await ElMessageBox.prompt(
|
|
t('auth.account.passkey.namePrompt'),
|
|
t('auth.account.passkey.nameTitle'),
|
|
{ inputValue: t('auth.account.passkey.defaultName'), inputValidator: value => Boolean(value.trim()) || t('auth.account.nameRequired') },
|
|
)
|
|
actionLoading.value = true
|
|
const start = await http.post('/auth/passkeys/registration/options', {
|
|
currentPassword: password.value,
|
|
friendlyName: name.value.trim(),
|
|
})
|
|
const credential = await createPasskey(start.data.options)
|
|
await http.post('/auth/passkeys/registration/complete', {
|
|
ceremonyId: start.data.ceremonyId,
|
|
credential,
|
|
})
|
|
ElMessage.success(t('auth.account.passkey.added'))
|
|
await loadAccount()
|
|
} catch (error) {
|
|
if (error !== 'cancel' && error !== 'close' && !isPasskeyCancellation(error))
|
|
ElMessage.error(apiErrorMessage(error))
|
|
} finally {
|
|
actionLoading.value = false
|
|
}
|
|
}
|
|
|
|
async function renamePasskey(item: any) {
|
|
try {
|
|
const result = await ElMessageBox.prompt(t('auth.account.passkey.renamePrompt'), t('auth.account.passkey.renameTitle'), {
|
|
inputValue: item.friendlyName,
|
|
inputValidator: value => Boolean(value.trim()) || t('auth.account.nameRequired'),
|
|
})
|
|
await http.put(`/auth/passkeys/${item.id}`, { friendlyName: result.value.trim() })
|
|
await loadAccount()
|
|
} catch (error) {
|
|
if (error !== 'cancel' && error !== 'close') ElMessage.error(apiErrorMessage(error))
|
|
}
|
|
}
|
|
|
|
async function removePasskey(item: any) {
|
|
try {
|
|
const result = await ElMessageBox.prompt(
|
|
t('auth.account.passkey.deletePrompt', { name: item.friendlyName }),
|
|
t('auth.account.passkey.deleteTitle'),
|
|
{ inputType: 'password', type: 'warning', inputValidator: value => Boolean(value) || t('auth.account.passwordRequired') },
|
|
)
|
|
await http.delete(`/auth/passkeys/${item.id}`, { data: { currentPassword: result.value } })
|
|
ElMessage.success(t('auth.account.passkey.deleted'))
|
|
await loadAccount()
|
|
} catch (error) {
|
|
if (error !== 'cancel' && error !== 'close') ElMessage.error(apiErrorMessage(error))
|
|
}
|
|
}
|
|
|
|
async function setupTotp() {
|
|
try {
|
|
const result = await ElMessageBox.prompt(t('auth.account.totp.setupPrompt'), t('auth.account.totp.setupTitle'), { inputType: 'password', inputValidator: value => Boolean(value) || t('auth.account.passwordRequired') })
|
|
actionLoading.value = true
|
|
const { data } = await http.post('/auth/security/totp/setup', { currentPassword: result.value })
|
|
totp.setupUri = String(data.otpauthUri)
|
|
const confirm = await ElMessageBox.prompt(t('auth.account.totp.confirmPrompt', { secret: data.secret }), t('auth.account.totp.confirmTitle'), { inputValidator: value => Boolean(value) || t('auth.account.codeRequired') })
|
|
const enabled = await http.post('/auth/security/totp/enable', { code: confirm.value })
|
|
totp.enabled = true
|
|
totp.recoveryCodes = enabled.data.recoveryCodes
|
|
ElMessage.success(t('auth.account.totp.enabledMessage'))
|
|
} catch (error) {
|
|
if (error !== 'cancel' && error !== 'close') ElMessage.error(apiErrorMessage(error))
|
|
} finally { actionLoading.value = false }
|
|
}
|
|
|
|
async function disableTotp() {
|
|
try {
|
|
const result = await ElMessageBox.prompt(t('auth.account.totp.disablePrompt'), t('auth.account.totp.disable'), { inputType: 'password', type: 'warning', inputValidator: value => Boolean(value) || t('auth.account.passwordRequired') })
|
|
actionLoading.value = true
|
|
await http.post('/auth/security/totp/disable', { currentPassword: result.value })
|
|
totp.enabled = false
|
|
ElMessage.success(t('auth.account.totp.disabledMessage'))
|
|
} catch (error) {
|
|
if (error !== 'cancel' && error !== 'close') ElMessage.error(apiErrorMessage(error))
|
|
} finally { actionLoading.value = false }
|
|
}
|
|
|
|
async function startBinding() {
|
|
actionLoading.value = true
|
|
try {
|
|
const nativeState = beginNativeSsoLogin()
|
|
const { data } = await http.post('/auth/sso/prepare-binding', null, {
|
|
params: { nativeApp: isNativeApp(), nativeState },
|
|
})
|
|
await openSsoLogin(String(data.loginUrl))
|
|
} catch (error) {
|
|
ElMessage.error(apiErrorMessage(error))
|
|
actionLoading.value = false
|
|
}
|
|
}
|
|
|
|
async function unbind() {
|
|
try {
|
|
const result = await ElMessageBox.prompt(
|
|
t('auth.account.sso.unbindPrompt'),
|
|
t('auth.account.sso.unbindTitle'),
|
|
{
|
|
inputType: 'password',
|
|
inputPlaceholder: t('auth.account.sso.passwordPlaceholder'),
|
|
inputValidator: (value) => Boolean(value) || t('auth.account.passwordRequired'),
|
|
confirmButtonText: t('auth.account.sso.confirmUnbind'),
|
|
cancelButtonText: t('auth.account.cancel'),
|
|
type: 'warning',
|
|
},
|
|
)
|
|
actionLoading.value = true
|
|
await http.post('/auth/sso/unbind', { password: result.value })
|
|
ElMessage.success(t('auth.account.sso.unboundMessage'))
|
|
await loadAccount()
|
|
} catch (error) {
|
|
if (error === 'cancel' || error === 'close') return
|
|
ElMessage.error(apiErrorMessage(error))
|
|
} finally {
|
|
actionLoading.value = false
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
const ssoError = String(route.query.ssoError ?? '')
|
|
if (ssoError) {
|
|
const key = `auth.account.ssoErrors.${ssoError}`
|
|
ElMessage.error(t(te(key) ? key : 'auth.account.ssoErrors.unknown'))
|
|
await router.replace('/account')
|
|
}
|
|
await loadAccount()
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<section class="account-page">
|
|
<header class="account-heading">
|
|
<div>
|
|
<span class="eyebrow">ACCOUNT & SECURITY</span>
|
|
<h1>{{ t('auth.account.title') }}</h1>
|
|
<p>{{ t('auth.account.description') }}</p>
|
|
</div>
|
|
<div class="account-avatar">{{ auth.user?.displayName?.slice(0, 1) ?? t('auth.account.avatarFallback') }}</div>
|
|
</header>
|
|
|
|
<div class="account-grid">
|
|
<article class="account-card identity-card">
|
|
<div class="card-title">
|
|
<div>
|
|
<span>{{ t('auth.account.basicInfo') }}</span>
|
|
<h2>{{ auth.user?.displayName }}</h2>
|
|
</div>
|
|
<el-tag type="success" effect="light">{{ t('auth.account.enabled') }}</el-tag>
|
|
</div>
|
|
<dl>
|
|
<div>
|
|
<dt>{{ t('auth.account.username') }}</dt>
|
|
<dd>{{ auth.user?.userName }}</dd>
|
|
</div>
|
|
<div>
|
|
<dt>{{ t('auth.account.roles') }}</dt>
|
|
<dd class="role-list">
|
|
<el-tag v-for="role in auth.user?.roles" :key="role" effect="plain">{{ translatedCode('auth.account.role', role) }}</el-tag>
|
|
</dd>
|
|
</div>
|
|
<div>
|
|
<dt>{{ t('auth.account.dataScope') }}</dt>
|
|
<dd>{{ translatedCode('auth.account.scope', auth.user?.effectiveDataScope ?? '') }}</dd>
|
|
</div>
|
|
</dl>
|
|
</article>
|
|
|
|
<article class="account-card sso-card">
|
|
<div class="card-title"><div><span>{{ t('auth.account.totp.label') }}</span><h2>{{ t('auth.account.totp.title') }}</h2></div><el-tag :type="totp.enabled ? 'success' : 'info'">{{ t(totp.enabled ? 'auth.account.statusEnabled' : 'auth.account.statusDisabled') }}</el-tag></div>
|
|
<p class="sso-description">{{ t('auth.account.totp.description') }}</p>
|
|
<el-button v-if="!totp.enabled" type="primary" :loading="actionLoading" @click="setupTotp">{{ t('auth.account.totp.enable') }}</el-button>
|
|
<el-button v-else type="danger" plain :loading="actionLoading" @click="disableTotp">{{ t('auth.account.totp.disable') }}</el-button>
|
|
<el-alert v-if="totp.recoveryCodes.length" :title="t('auth.account.totp.recovery')" type="warning" :closable="false" show-icon><template #default><code>{{ totp.recoveryCodes.join(' ') }}</code></template></el-alert>
|
|
</article>
|
|
|
|
<article v-loading="loading" class="account-card sso-card passkey-card">
|
|
<div class="card-title"><div><span>{{ t('auth.account.passkey.label') }}</span><h2>Passkey</h2></div><el-tag :type="passkey.enabled ? 'success' : 'warning'">{{ t(passkey.enabled ? 'auth.account.statusEnabled' : 'auth.account.notConfigured') }}</el-tag></div>
|
|
<p class="sso-description">{{ t('auth.account.passkey.description') }}</p>
|
|
<el-alert v-if="passkey.enabled && !passkey.supported" type="warning" :closable="false" :title="t('auth.account.passkey.unsupported')" />
|
|
<div v-if="passkey.items.length" class="passkey-list">
|
|
<div v-for="item in passkey.items" :key="item.id">
|
|
<span><b>{{ item.friendlyName }}</b><small>{{ t('auth.account.passkey.addedAt', { date: formatDate(item.createdAt) }) }}<template v-if="item.lastUsedAt"> · {{ t('auth.account.passkey.lastUsed', { date: formatDate(item.lastUsedAt) }) }}</template></small></span>
|
|
<span><el-button link @click="renamePasskey(item)">{{ t('auth.account.passkey.rename') }}</el-button><el-button link type="danger" @click="removePasskey(item)">{{ t('auth.account.passkey.delete') }}</el-button></span>
|
|
</div>
|
|
</div>
|
|
<el-button v-if="passkey.enabled && passkey.supported" type="primary" :loading="actionLoading" @click="addPasskey">{{ t('auth.account.passkey.add') }}</el-button>
|
|
</article>
|
|
|
|
<article v-loading="loading" class="account-card sso-card">
|
|
<div class="card-title">
|
|
<div>
|
|
<span>{{ t('auth.account.sso.label') }}</span>
|
|
<h2>{{ sso.providerDisplayName || t('auth.sso.defaultProvider') }}</h2>
|
|
</div>
|
|
<el-tag v-if="sso.isBound" type="success">{{ t('auth.account.bound') }}</el-tag>
|
|
<el-tag v-else-if="sso.enabled" type="info">{{ t('auth.account.unbound') }}</el-tag>
|
|
<el-tag v-else type="warning">{{ t('auth.account.statusDisabled') }}</el-tag>
|
|
</div>
|
|
|
|
<p v-if="sso.isBound" class="sso-description">
|
|
{{ t('auth.account.sso.boundDescription') }}
|
|
</p>
|
|
<p v-else-if="sso.enabled" class="sso-description">
|
|
{{ t('auth.account.sso.unboundDescription') }}
|
|
</p>
|
|
<p v-else class="sso-description">{{ t('auth.account.sso.disabledDescription') }}</p>
|
|
|
|
<el-button
|
|
v-if="sso.enabled && !sso.isBound"
|
|
type="primary"
|
|
:loading="actionLoading"
|
|
@click="startBinding"
|
|
>
|
|
{{ t('auth.account.sso.bind') }}
|
|
</el-button>
|
|
<el-button
|
|
v-else-if="sso.isBound"
|
|
type="danger"
|
|
plain
|
|
:loading="actionLoading"
|
|
@click="unbind"
|
|
>
|
|
{{ t('auth.account.sso.unbind') }}
|
|
</el-button>
|
|
|
|
<details v-if="sso.enabled && sso.callbackUrl" class="callback-details">
|
|
<summary>{{ t('auth.account.sso.adminReference') }}</summary>
|
|
<p>{{ t('auth.account.sso.redirectHint') }}</p>
|
|
<code>{{ sso.callbackUrl }}</code>
|
|
</details>
|
|
</article>
|
|
</div>
|
|
</section>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.account-page { display: grid; gap: 22px; }
|
|
.account-heading { display: flex; align-items: center; justify-content: space-between; padding: 26px 30px; border-radius: 18px; background: linear-gradient(125deg, #173a50, #176b87 65%, #2e9b91); color: white; box-shadow: 0 18px 45px rgba(23, 58, 80, .18); }
|
|
.account-heading h1 { margin: 7px 0 5px; font-family: "STZhongsong", "Songti SC", serif; font-size: 30px; }
|
|
.account-heading p { margin: 0; color: rgba(255,255,255,.72); }
|
|
.account-heading .eyebrow { color: #8ee4d8; }
|
|
.account-avatar { display: grid; place-items: center; width: 68px; height: 68px; border: 1px solid rgba(255,255,255,.34); border-radius: 22px; background: rgba(255,255,255,.12); font-size: 28px; font-weight: 700; }
|
|
.account-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
|
|
.account-card { min-width: 0; min-height: 310px; padding: 28px; border: 1px solid #e2e8ee; border-radius: 16px; background: white; box-shadow: 0 12px 34px rgba(35, 57, 78, .07); }
|
|
.card-title { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; padding-bottom: 20px; border-bottom: 1px solid #edf0f3; }
|
|
.card-title span { color: #73808d; font-size: 12px; }
|
|
.card-title h2 { margin: 6px 0 0; color: #263445; font-size: 21px; }
|
|
dl { margin: 6px 0 0; }
|
|
dl > div { display: grid; grid-template-columns: 100px 1fr; gap: 18px; padding: 16px 0; border-bottom: 1px solid #f0f2f4; }
|
|
dt { color: #627184; font-size: 13px; }
|
|
dd { margin: 0; color: #263445; font-weight: 650; }
|
|
.role-list { display: flex; flex-wrap: wrap; gap: 7px; }
|
|
.sso-description { min-height: 54px; margin: 22px 0; color: #65717e; line-height: 1.7; }
|
|
.callback-details { margin-top: 24px; color: #65717e; font-size: 12px; }
|
|
.callback-details summary { cursor: pointer; color: #315b73; font-weight: 650; }
|
|
.callback-details p { margin: 10px 0 7px; }
|
|
.callback-details code { display: block; overflow-wrap: anywhere; padding: 10px 12px; border-radius: 8px; background: #f4f7f9; color: #176b87; }
|
|
.passkey-list { display: grid; gap: 8px; margin: 0 0 18px; }
|
|
.passkey-list > div { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px; border-radius: 10px; background: #f5f8fa; }
|
|
.passkey-list span:first-child { display: grid; gap: 3px; min-width: 0; }
|
|
.passkey-list b { overflow: hidden; color: #263445; text-overflow: ellipsis; white-space: nowrap; }
|
|
.passkey-list small { color: #7b8792; }
|
|
@media (max-width: 1100px) {
|
|
.account-grid { grid-template-columns: 1fr; }
|
|
.account-heading { padding: 22px; }
|
|
.account-avatar { width: 56px; height: 56px; border-radius: 18px; }
|
|
.account-card { min-height: 0; padding: 22px; }
|
|
}
|
|
</style>
|