From 31d656b6a17c6c0e6b3c68d6b1d2c57939d4dd03 Mon Sep 17 00:00:00 2001 From: Megghy Date: Tue, 12 Mar 2024 17:58:12 +0800 Subject: [PATCH] fix auth login logic --- src/store/useAuthStore.ts | 16 ++++++++-------- src/views/BiliAuthView.vue | 16 ++++++++++------ src/views/pointViews/PointUserLayout.vue | 1 + 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/store/useAuthStore.ts b/src/store/useAuthStore.ts index df43ee5..5480185 100644 --- a/src/store/useAuthStore.ts +++ b/src/store/useAuthStore.ts @@ -17,10 +17,10 @@ export const useAuthStore = defineStore('BiliAuth', () => { token: string }[] >('Bili.Auth.Tokens', []) - const biliToken = useStorage('Bili.Auth.Selected', null) + const currentToken = useStorage('Bili.Auth.Selected', null) const isLoading = ref(false) - const isAuthed = computed(() => biliToken.value != null && biliToken.value.length > 0) + const isAuthed = computed(() => currentToken.value != null && currentToken.value.length > 0) async function setCurrentAuth(token: string) { if (!token) { @@ -28,7 +28,7 @@ export const useAuthStore = defineStore('BiliAuth', () => { return } biliAuth.value = {} as BiliAuthModel - biliToken.value = token + currentToken.value = token await getAuthInfo() } @@ -44,7 +44,7 @@ export const useAuthStore = defineStore('BiliAuth', () => { if (index >= 0) { biliTokens.value[index] = { id: biliAuth.value.id, - token: biliToken.value, + token: currentToken.value, name: biliAuth.value.name, uId: biliAuth.value.userId, } @@ -52,7 +52,7 @@ export const useAuthStore = defineStore('BiliAuth', () => { } else { biliTokens.value.push({ id: biliAuth.value.id, - token: biliToken.value, + token: currentToken.value, name: biliAuth.value.name, uId: biliAuth.value.userId, }) @@ -75,14 +75,14 @@ export const useAuthStore = defineStore('BiliAuth', () => { function QueryBiliAuthGetAPI(url: string, params?: any, headers?: [string, string][]) { headers ??= [] if (headers.find((h) => h[0] == 'Bili-Auth') == null) { - headers.push(['Bili-Auth', biliToken.value ?? '']) + headers.push(['Bili-Auth', currentToken.value ?? '']) } return QueryGetAPI(url, params, headers) } function QueryBiliAuthPostAPI(url: string, body?: unknown, headers?: [string, string][]) { headers ??= [] if (headers.find((h) => h[0] == 'Bili-Auth') == null) { - headers.push(['Bili-Auth', biliToken.value ?? '']) + headers.push(['Bili-Auth', currentToken.value ?? '']) } return QueryPostAPI(url, body, headers) } @@ -123,7 +123,7 @@ export const useAuthStore = defineStore('BiliAuth', () => { return { biliAuth, - biliToken, + biliToken: currentToken, biliTokens, isLoading, isAuthed, diff --git a/src/views/BiliAuthView.vue b/src/views/BiliAuthView.vue index a5cef5b..7a8f9fd 100644 --- a/src/views/BiliAuthView.vue +++ b/src/views/BiliAuthView.vue @@ -24,6 +24,7 @@ import { } from 'naive-ui' import { computed, onMounted, ref } from 'vue' import { v4 as uuidv4 } from 'uuid' +import { useAuthStore } from '@/store/useAuthStore' type AuthStartModel = { code: string @@ -35,11 +36,12 @@ type AuthStartModel = { const message = useMessage() const guidKey = useStorage('Bili.Auth.Key', uuidv4()) -const biliToken = useStorage('Bili.Auth.Token', null) +const currentToken = useStorage('Bili.Auth.Selected', null) +const useAuth = useAuthStore() const startModel = ref() -const currentStep = ref(biliToken.value ? 2 : 0) +const currentStep = ref(currentToken.value ? 2 : 0) const isStart = computed(() => { return currentStep.value > 0 @@ -76,7 +78,9 @@ async function checkStatus() { clearInterval(timer.value) message.success('认证成功') - biliToken.value = data.data as string + currentToken.value = data.data as string + useAuth.getAuthInfo() + currentStep.value = 2 return true @@ -106,7 +110,7 @@ function copyCode() { } onMounted(async () => { - if (!biliToken.value) { + if (!currentToken.value) { if (await checkStatus()) { currentStep.value = 1 timer.value = setInterval(checkStatus, 5000) @@ -182,7 +186,7 @@ onMounted(async () => { 你的登陆链接为: @@ -195,7 +199,7 @@ onMounted(async () => { () => { currentStep = 0 //@ts-ignore - biliToken = null + currentToken = null guidKey = uuidv4() } " diff --git a/src/views/pointViews/PointUserLayout.vue b/src/views/pointViews/PointUserLayout.vue index 17b340a..f913b25 100644 --- a/src/views/pointViews/PointUserLayout.vue +++ b/src/views/pointViews/PointUserLayout.vue @@ -110,6 +110,7 @@ onMounted(async () => { const route = useRoute() if (route.query.auth) { useAuth.biliToken = route.query.auth as string + console.log(route.query.auth) } if (biliAuth.value?.id < 0) { isLoading.value = true