110 lines
3.4 KiB
Vue
110 lines
3.4 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 { safeAuthRedirect } from '../utils/navigate'
|
|
import LocaleSwitcher from '../components/LocaleSwitcher.vue'
|
|
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const auth = useAuthStore()
|
|
const { t } = useI18n()
|
|
const loading = ref(false)
|
|
const checking = ref(true)
|
|
const binding = reactive({
|
|
providerDisplayName: '',
|
|
externalUserName: '',
|
|
})
|
|
const form = reactive({ userName: '', password: '' })
|
|
|
|
async function submit() {
|
|
if (!form.userName.trim() || !form.password) {
|
|
ElMessage.warning(t('auth.sso.required'))
|
|
return
|
|
}
|
|
|
|
loading.value = true
|
|
try {
|
|
await auth.bindSso(String(route.query.code ?? ''), form.userName, form.password)
|
|
ElMessage.success(t('auth.sso.success'))
|
|
await router.replace(safeAuthRedirect(route.query.redirect))
|
|
} catch (error) {
|
|
ElMessage.error(apiErrorMessage(error))
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
const code = String(route.query.code ?? '')
|
|
if (!code) {
|
|
await router.replace({ name: 'login', query: { ssoError: 'authentication_failed' } })
|
|
return
|
|
}
|
|
|
|
try {
|
|
const { data } = await http.get('/auth/sso/binding', { params: { code } })
|
|
binding.providerDisplayName = String(data.providerDisplayName || binding.providerDisplayName)
|
|
binding.externalUserName = String(data.externalUserName || '')
|
|
} catch (error) {
|
|
ElMessage.error(apiErrorMessage(error))
|
|
await router.replace({ name: 'login', query: { ssoError: 'binding_expired' } })
|
|
} finally {
|
|
checking.value = false
|
|
}
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<main class="sso-binding-page">
|
|
<section v-loading="checking" class="sso-binding-card">
|
|
<LocaleSwitcher class="binding-locale-switcher" />
|
|
<div class="brand-mark" aria-hidden="true">
|
|
<span v-for="index in 9" :key="index" />
|
|
</div>
|
|
<span class="eyebrow">{{ t('auth.sso.firstUse') }}</span>
|
|
<h1>{{ t('auth.sso.bindTitle') }}</h1>
|
|
<p class="binding-description">
|
|
{{ t('auth.sso.verified', { provider: binding.providerDisplayName || t('auth.sso.defaultProvider') }) }}
|
|
<strong v-if="binding.externalUserName">{{ binding.externalUserName }}</strong>{{ t('auth.sso.sentenceEnd') }}
|
|
{{ t('auth.sso.bindHint') }}
|
|
</p>
|
|
|
|
<form @submit.prevent="submit">
|
|
<label>
|
|
<span>{{ t('auth.sso.account') }}</span>
|
|
<el-input v-model="form.userName" size="large" autocomplete="username" />
|
|
</label>
|
|
<label>
|
|
<span>{{ t('auth.sso.password') }}</span>
|
|
<el-input
|
|
v-model="form.password"
|
|
size="large"
|
|
type="password"
|
|
show-password
|
|
autocomplete="current-password"
|
|
@keyup.enter="submit"
|
|
/>
|
|
</label>
|
|
<el-button
|
|
class="binding-submit"
|
|
type="primary"
|
|
size="large"
|
|
native-type="submit"
|
|
:loading="loading"
|
|
:disabled="checking"
|
|
>
|
|
{{ t('auth.sso.bindSubmit') }}
|
|
</el-button>
|
|
</form>
|
|
|
|
<p class="binding-security-note">
|
|
{{ t('auth.sso.securityNote') }}
|
|
</p>
|
|
<router-link to="/login">{{ t('auth.sso.cancel') }}</router-link>
|
|
</section>
|
|
</main>
|
|
</template>
|