209 lines
7.6 KiB
Vue
209 lines
7.6 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, reactive, ref } from 'vue'
|
|
import { useI18n } from 'vue-i18n'
|
|
import { useRoute, useRouter } from 'vue-router'
|
|
import { apiErrorMessage } from '../api/http'
|
|
import { useAuthStore } from '../stores/auth'
|
|
import http from '../api/http'
|
|
import { isNativeApp } from '../auth/session'
|
|
import { beginNativeSsoLogin, openSsoLogin } from '../services/nativeAppLinks'
|
|
import { safeAuthRedirect } from '../utils/navigate'
|
|
import LoginCaptcha from '../components/LoginCaptcha.vue'
|
|
import { branding } from '../config/branding'
|
|
import { isPasskeyCancellation, passkeysSupported } from '../services/passkeys'
|
|
import LocaleSwitcher from '../components/LocaleSwitcher.vue'
|
|
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const auth = useAuthStore()
|
|
const { t, te } = useI18n()
|
|
const loading = ref(false)
|
|
const twoFactorTicket = ref('')
|
|
const captchaId = ref('')
|
|
const captchaCode = ref('')
|
|
const captcha = ref<{ refresh: () => Promise<void> } | null>(null)
|
|
const ssoLoading = ref(false)
|
|
const passkeyLoading = ref(false)
|
|
const passkey = reactive({ enabled: false, supported: passkeysSupported() })
|
|
const sso = reactive({ enabled: false, displayName: '' })
|
|
const ssoDisplayName = computed(() => sso.displayName || t('auth.sso.defaultProvider'))
|
|
const form = reactive({
|
|
userName: String(route.query.userName ?? ''),
|
|
password: '',
|
|
totpCode: '',
|
|
})
|
|
|
|
async function submit() {
|
|
loading.value = true
|
|
try {
|
|
const result = await auth.login(form.userName, form.password, captchaId.value, captchaCode.value)
|
|
if (result.twoFactorTicket) {
|
|
twoFactorTicket.value = result.twoFactorTicket
|
|
return
|
|
}
|
|
await router.replace(safeAuthRedirect(route.query.redirect))
|
|
} catch (error) {
|
|
captchaCode.value = ''
|
|
await captcha.value?.refresh()
|
|
ElMessage.error(apiErrorMessage(error))
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function submitTotp() {
|
|
loading.value = true
|
|
try {
|
|
await auth.completeTotpLogin(twoFactorTicket.value, form.totpCode)
|
|
await router.replace(safeAuthRedirect(route.query.redirect))
|
|
} catch (error) {
|
|
ElMessage.error(apiErrorMessage(error))
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function startSso() {
|
|
ssoLoading.value = true
|
|
const apiBaseUrl = String(import.meta.env.VITE_API_BASE_URL ?? '/api').replace(/\/$/, '')
|
|
const redirect = safeAuthRedirect(route.query.redirect)
|
|
const nativeState = beginNativeSsoLogin()
|
|
try {
|
|
if (!captchaId.value || !captchaCode.value) return
|
|
await openSsoLogin(
|
|
`${apiBaseUrl}/auth/sso/login?returnUrl=${encodeURIComponent(redirect)}&nativeApp=${isNativeApp()}&captchaId=${encodeURIComponent(captchaId.value)}&captchaCode=${encodeURIComponent(captchaCode.value)}${nativeState ? `&nativeState=${encodeURIComponent(nativeState)}` : ''}`,
|
|
)
|
|
} catch (error) {
|
|
ssoLoading.value = false
|
|
ElMessage.error(apiErrorMessage(error))
|
|
}
|
|
}
|
|
|
|
async function submitPasskey() {
|
|
passkeyLoading.value = true
|
|
try {
|
|
await auth.loginWithPasskey()
|
|
await router.replace(safeAuthRedirect(route.query.redirect))
|
|
} catch (error) {
|
|
if (!isPasskeyCancellation(error)) ElMessage.error(apiErrorMessage(error))
|
|
} finally {
|
|
passkeyLoading.value = false
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
const ssoError = String(route.query.ssoError ?? '')
|
|
if (ssoError) {
|
|
const key = `auth.ssoErrors.${ssoError}`
|
|
ElMessage.error(t(te(key) ? key : 'auth.ssoErrors.unknown'))
|
|
}
|
|
try {
|
|
const passkeyResult = await http.get('/auth/passkeys/settings')
|
|
passkey.enabled = Boolean(passkeyResult.data.enabled)
|
|
const { data } = await http.get('/auth/sso/settings')
|
|
sso.enabled = Boolean(data.enabled)
|
|
sso.displayName = String(data.displayName || sso.displayName)
|
|
} catch {
|
|
sso.enabled = false
|
|
}
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<main class="login-page">
|
|
<section class="login-story">
|
|
<div class="story-brand">
|
|
<div class="brand-mark large" aria-hidden="true">
|
|
<span v-for="index in 9" :key="index" />
|
|
</div>
|
|
<div>
|
|
<strong>{{ branding.shortName }}</strong>
|
|
<small>{{ branding.tagline }}</small>
|
|
</div>
|
|
</div>
|
|
<div class="story-copy">
|
|
<span class="eyebrow">{{ branding.productName }}</span>
|
|
<h1>{{ t('auth.login.storyTitleLine1') }}<br />{{ t('auth.login.storyTitleLine2') }}</h1>
|
|
<p>{{ t('auth.login.storyDescription') }}</p>
|
|
</div>
|
|
<div class="schedule-signature" aria-hidden="true">
|
|
<div v-for="item in 20" :key="item" :class="{ active: [3, 8, 9, 14, 18].includes(item) }">
|
|
<span v-if="[3, 8, 9, 14, 18].includes(item)">{{ t('auth.login.scheduleLabel') }}</span>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="login-panel">
|
|
<LocaleSwitcher class="public-locale-switcher login-locale-switcher" />
|
|
<form v-if="!twoFactorTicket" class="login-form" @submit.prevent="submit">
|
|
<div class="form-intro">
|
|
<span>{{ t('auth.login.welcome') }}</span>
|
|
<h2>{{ t('auth.login.title') }}</h2>
|
|
<p>{{ t('auth.login.hint') }}</p>
|
|
</div>
|
|
<label>
|
|
<span>{{ t('auth.login.username') }}</span>
|
|
<el-input v-model="form.userName" size="large" autocomplete="username" />
|
|
</label>
|
|
<label>
|
|
<span>{{ t('auth.login.password') }}</span>
|
|
<el-input
|
|
v-model="form.password"
|
|
size="large"
|
|
type="password"
|
|
show-password
|
|
autocomplete="current-password"
|
|
@keyup.enter="submit"
|
|
/>
|
|
</label>
|
|
<LoginCaptcha
|
|
ref="captcha"
|
|
@change="(id, code) => { captchaId = id; captchaCode = code }"
|
|
/>
|
|
<el-button
|
|
class="login-submit"
|
|
type="primary"
|
|
size="large"
|
|
native-type="submit"
|
|
:loading="loading"
|
|
:disabled="!captchaId || !captchaCode"
|
|
>
|
|
{{ t('auth.login.submit') }}
|
|
</el-button>
|
|
<div v-if="passkey.enabled && passkey.supported" class="login-divider"><span>{{ t('auth.login.or') }}</span></div>
|
|
<el-button
|
|
v-if="passkey.enabled && passkey.supported"
|
|
class="sso-login-submit"
|
|
size="large"
|
|
:loading="passkeyLoading"
|
|
@click="submitPasskey"
|
|
>
|
|
{{ t('auth.login.passkey') }}
|
|
</el-button>
|
|
<div v-if="sso.enabled" class="login-divider"><span>{{ t('auth.login.or') }}</span></div>
|
|
<el-button
|
|
v-if="sso.enabled"
|
|
class="sso-login-submit"
|
|
size="large"
|
|
:loading="ssoLoading"
|
|
:disabled="!captchaId || !captchaCode"
|
|
@click="startSso"
|
|
>
|
|
{{ t('auth.login.sso', { provider: ssoDisplayName }) }}
|
|
</el-button>
|
|
<router-link class="public-timetable-link" to="/timetable">{{ t('auth.login.publicTimetable') }}</router-link>
|
|
<router-link class="account-activation-link" to="/activate">{{ t('auth.login.activate') }}</router-link>
|
|
</form>
|
|
<form v-else class="login-form" @submit.prevent="submitTotp">
|
|
<div class="form-intro">
|
|
<span>{{ t('auth.login.twoFactor') }}</span><h2>{{ t('auth.login.verificationTitle') }}</h2>
|
|
<p>{{ t('auth.login.verificationHint') }}</p>
|
|
</div>
|
|
<label><span>{{ t('auth.login.verificationCode') }}</span><el-input v-model="form.totpCode" size="large" inputmode="numeric" autocomplete="one-time-code" /></label>
|
|
<el-button class="login-submit" type="primary" size="large" native-type="submit" :loading="loading">{{ t('auth.login.verify') }}</el-button>
|
|
<el-button text @click="twoFactorTicket = ''">{{ t('auth.login.back') }}</el-button>
|
|
</form>
|
|
</section>
|
|
</main>
|
|
</template>
|