|
| 1 | +<template> |
| 2 | + <div class="page"> |
| 3 | + <UIForm class="form" :form="form" @submit="handleSubmit.fn"> |
| 4 | + <h1 class="title">{{ $t(title) }}</h1> |
| 5 | + <UIFormItem path="token"> |
| 6 | + <UITextInput |
| 7 | + v-model:value="form.value.token" |
| 8 | + class="input" |
| 9 | + type="textarea" |
| 10 | + :placeholder="$t({ en: 'Paste token here', zh: '在此粘贴 Token' })" |
| 11 | + /> |
| 12 | + </UIFormItem> |
| 13 | + <footer class="footer"> |
| 14 | + <UIButton type="boring" @click="handleCancel"> |
| 15 | + {{ $t({ en: 'Cancel', zh: '取消' }) }} |
| 16 | + </UIButton> |
| 17 | + <UIButton type="primary" html-type="submit" :loading="handleSubmit.isLoading.value"> |
| 18 | + {{ buttonText }} |
| 19 | + </UIButton> |
| 20 | + </footer> |
| 21 | + </UIForm> |
| 22 | + </div> |
| 23 | +</template> |
| 24 | +<script setup lang="ts"> |
| 25 | +import { computed, ref } from 'vue' |
| 26 | +import { useRouter } from 'vue-router' |
| 27 | +import { useI18n } from '@/utils/i18n' |
| 28 | +import { usePageTitle } from '@/utils/utils' |
| 29 | +import { useMessageHandle } from '@/utils/exception' |
| 30 | +import { useUserStore, type UserInfo } from '@/stores/user' |
| 31 | +import { UIForm, UIFormItem, UITextInput, UIButton, useForm } from '@/components/ui' |
| 32 | +
|
| 33 | +const title = { |
| 34 | + en: 'Sign in with token', |
| 35 | + zh: '使用 Token 登录' |
| 36 | +} |
| 37 | +
|
| 38 | +usePageTitle(title) |
| 39 | +
|
| 40 | +const router = useRouter() |
| 41 | +const userStore = useUserStore() |
| 42 | +const i18n = useI18n() |
| 43 | +
|
| 44 | +const userInfo = ref<UserInfo | null>(null) |
| 45 | +const buttonText = computed(() => { |
| 46 | + if (userInfo.value == null) return i18n.t({ en: 'Sign in', zh: '登录' }) |
| 47 | + const username = userInfo.value.displayName || userInfo.value.name |
| 48 | + return i18n.t({ |
| 49 | + en: `Sign in as ${username}`, |
| 50 | + zh: `以 ${username} 登录` |
| 51 | + }) |
| 52 | +}) |
| 53 | +
|
| 54 | +const form = useForm({ |
| 55 | + token: ['', validateToken] |
| 56 | +}) |
| 57 | +
|
| 58 | +function validateToken(token: string) { |
| 59 | + userInfo.value = null |
| 60 | + token = token.trim() |
| 61 | + if (token === '') |
| 62 | + return i18n.t({ |
| 63 | + en: 'Token is required', |
| 64 | + zh: '请提供 Token' |
| 65 | + }) |
| 66 | + try { |
| 67 | + userInfo.value = userStore.parseAccessToken(token) |
| 68 | + } catch (e) { |
| 69 | + return i18n.t({ |
| 70 | + en: 'Invalid token: ' + e, |
| 71 | + zh: '无效的 Token:' + e |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | +
|
| 76 | +function handleCancel() { |
| 77 | + router.push('/') |
| 78 | +} |
| 79 | +
|
| 80 | +const handleSubmit = useMessageHandle( |
| 81 | + async () => { |
| 82 | + const token = form.value.token.trim() |
| 83 | + userStore.signInWithAccessToken(token) |
| 84 | + router.push('/') |
| 85 | + }, |
| 86 | + { |
| 87 | + en: 'Failed to signin', |
| 88 | + zh: '登录失败' |
| 89 | + } |
| 90 | +) |
| 91 | +</script> |
| 92 | +<style scoped lang="scss"> |
| 93 | +.page { |
| 94 | + width: 100%; |
| 95 | + height: 100%; |
| 96 | + display: flex; |
| 97 | + justify-content: center; |
| 98 | + align-items: center; |
| 99 | +} |
| 100 | +
|
| 101 | +.form { |
| 102 | + width: 320px; |
| 103 | + display: flex; |
| 104 | + flex-direction: column; |
| 105 | +} |
| 106 | +
|
| 107 | +.title { |
| 108 | + margin-bottom: 1em; |
| 109 | + font-size: 16px; |
| 110 | + text-align: center; |
| 111 | +} |
| 112 | +
|
| 113 | +.input { |
| 114 | + justify-self: stretch; |
| 115 | + height: 160px; |
| 116 | +} |
| 117 | +
|
| 118 | +.footer { |
| 119 | + margin-top: 1em; |
| 120 | + display: flex; |
| 121 | + justify-content: center; |
| 122 | + gap: 1em; |
| 123 | +} |
| 124 | +</style> |
0 commit comments