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